Connecting Tech Pros Worldwide Help | Site Map

PHP strange error with MY_SQL database

  #1  
Old July 17th, 2005, 10:59 AM
asra_baig@rocketmail.com
Guest
 
Posts: n/a
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']

  #2  
Old July 17th, 2005, 10:59 AM
Pedro Graca
Guest
 
Posts: n/a

re: PHP strange error with MY_SQL database


asra_baig@rocketmail.com wrote:[color=blue]
> 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)[/color]


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

<snip>
[color=blue]
> $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)))[/color]

<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!
  #3  
Old July 17th, 2005, 10:59 AM
Dani CS
Guest
 
Posts: n/a

re: PHP strange error with MY_SQL database


asra_baig@rocketmail.com wrote:[color=blue]
> 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)
>[/color]

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

<snip>
Closed Thread