PHP strange error with MY_SQL database

This is a discussion on PHP strange error with MY_SQL database within the PHP Language forums, part of the PHP Programming Forums category; I have a MySQL database and I'm able to access it correctly. I am also able to execute select, ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-13-2004
asra_baig@rocketmail.com
 
Posts: n/a
Default PHP strange error with MY_SQL database

I have a MySQL database and I'm able to access it correctly. I am also
able to execute select, update and insert queries, however I am getting
warnings saying:
"supplied argument is not a valid MySQL result resource" and the result
set returned is empty (for queries other than select).
Strangely, my die function includes the query but the query is not
printed. I've however printed the queries before executing and they are
fine too. Can anyone discover the problem? The code is as below:
(please also note that as the code below has @'s the warnings are not
displayed)


if(($_POST['ADD'] == "1") || ($_POST['ADD'] == "0")) {

session_start();
$cart_id = GetCartId(); // my own function, works fine

$merchants = explode("_", $_GET['merchants']);
$merchant_id = $merchants[(int)$_POST['merchantid']];

$connection = db_connect(); // my own function, works fine
if($connection) {
$where_clause = " WHERE `Session`= '" . $cart_id . "' AND `Product ID`
= " . $_POST['productid'];
$cart_query = "SELECT * from `Shopping`" . $where_clause;
$cart_result = @mysql_query($cart_query, $connection) or
die(mysql_error() . ": $cart_query");
$cart_row = @mysql_fetch_array($cart_result);

$result = null;
if($_POST['ADD']=="1") {
if($cart_row) {
$update_query = "UPDATE `Shopping` SET `Merchant ID` = " .
$merchant_id . " , `quantity`= " . $_POST['quantity'] .
$where_clause;
echo $update_query;
$update_result = @mysql_query($update_query, $connection) or
die(mysql_error() . ": $update_query");
/* Error comes here */
if(($result = @mysql_fetch_array($update_result)))
{}

} else {
$add_query = "INSERT INTO `Shopping` ( `Session`, `Product ID`,
`Merchant ID`, `quantity` ) VALUES ( '" . $cart_id . "', "
..$_POST['productid'] . ", " . $merchant_id . ", " . $_POST['quantity']
.. ")";
$add_result = @mysql_query($add_query, $connection) or die
(mysql_error() . ": $add_query");
/* Error comes here */
if(($result = @mysql_fetch_array($add_result)))
{}
}
} else {
//delete
$delete_query = "DELETE FROM `Shopping` " . $where_clause;
$delete_result = @mysql_query($delete_query, $connection) or die
(mysql_error() . ": $delete_query");
if(($result=@mysql_fetch_array($delete_result)))
{}
}

} //if connection
} // if $_POST['ADD']

Reply With Quote
  #2 (permalink)  
Old 12-13-2004
Pedro Graca
 
Posts: n/a
Default Re: PHP strange error with MY_SQL database

asra_baig@rocketmail.com wrote:
> I have a MySQL database and I'm able to access it correctly. I am also
> able to execute select, update and insert queries, however I am getting
> warnings saying:
> "supplied argument is not a valid MySQL result resource" and the result
> set returned is empty (for queries other than select).
> Strangely, my die function includes the query but the query is not
> printed. I've however printed the queries before executing and they are
> fine too. Can anyone discover the problem? The code is as below:
> (please also note that as the code below has @'s the warnings are not
> displayed)



UGH! UUUGGHHHHH!!!!!!! OUCH!!!
Please, indent your code before pasting it to the newsgroups.

<snip>

> $update_query = "UPDATE `Shopping` SET `Merchant ID` = " .
> $merchant_id . " , `quantity`= " . $_POST['quantity'] .
> $where_clause;
> echo $update_query;
> $update_result = @mysql_query($update_query, $connection) or
> die(mysql_error() . ": $update_query");
> /* Error comes here */
> if(($result = @mysql_fetch_array($update_result)))


<snip>

OUCH!!!!!!!! MY EYES HURT!!!!!!!!!!
Please, indent your code before pasting it to the newsgroups.


mysql_fetch_*() functions are only valid after a mysql_query() for
SELECT, SHOW, EXPLAIN or DESCRIBE.

for UPDATE, INSERT, DELETE, ... the mysql_query() is all you need.

*Read* the mysql_query() manual page.
http://www.php.net/mysql_query




Please, indent your code before pasting it to the newsgroups.

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Reply With Quote
  #3 (permalink)  
Old 12-13-2004
Dani CS
 
Posts: n/a
Default Re: PHP strange error with MY_SQL database

asra_baig@rocketmail.com wrote:
> I have a MySQL database and I'm able to access it correctly. I am also
> able to execute select, update and insert queries, however I am getting
> warnings saying:
> "supplied argument is not a valid MySQL result resource" and the result
> set returned is empty (for queries other than select).
> Strangely, my die function includes the query but the query is not
> printed. I've however printed the queries before executing and they are
> fine too. Can anyone discover the problem? The code is as below:
> (please also note that as the code below has @'s the warnings are not
> displayed)
>


Remove the @'s and correct the errors that you will get.

<snip>
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:22 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0