Saiv Posted October 1, 2013 Share Posted October 1, 2013 (edited) Shopping cart coding indx.php getting error, Cart Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in E:\wamp\www\cart\index.php on line 39 ----------------------------------------------------------------------------------------------------------------------------- CODE: <div id="side"><h1>Cart</h1><?phpif(isset($_SESSION['cart'])){$sql="select * from products where id_products IN {"; foreach($_SESSION['cart'] as $id => $value){ $sql .=$id . ","; } $sql =substr($sql,0,-1) . "} ORDER BY id_products ASC"; $query=mysql_query($sql);while($row=mysql_fetch_assoc($query)){?><p><?php echo $row['name']; echo "*". $_SESSION['cart'][$row['id_products']]['quantity']; ?></p><a href="index.php?page=cart">Go to cart</a><?php}}else{echo "<p>Your cart is empty.</br> Please add some item</p>";}?></div> Edited October 1, 2013 by Saiv Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 1, 2013 Share Posted October 1, 2013 (edited) Your query is failing and returning false. The IN clause uses parens () around the list of values not curly braces {}. Plus, using a foreach loop to create the list of values and then removing the last comma is a waste when you can do it all in a single line using implode(). //Ensure all values are integers and remove 0 values $prodIDs = array_filter(array_map('intval', $_SESSION['cart'])); //Create comma separated list $prodIDList = implode(', ', $prodIDs); $sql = "SELECT * FROM products WHERE id_products IN ($prodIDList) ORDER BY id_products ASC"; $query = mysql_query($sql); Edited October 1, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 9, 2013 Share Posted October 9, 2013 $row=mysql_fetch_assoc($query) should be @$row=mysql_fetch_assoc($query) it will ignore the error.. in php 5 and above if fetch is getting zero results then it happens.. so placing @ sign before it will resolve your question for further assistance contact me NO the error suppression should never be used, it does not ignore the error either. It will prevent the error from displaying, but PHP will also log the error too. Before using mysl_fetch_assoc you need to first check the query returned any results using mysql_num_rows. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 10, 2013 Share Posted October 10, 2013 NO the error suppression should never be used, it does not ignore the error either. It will prevent the error from displaying, but PHP will also log the error too. Before using mysl_fetch_assoc you need to first check the query returned any results using mysql_num_rows. I agree with you that suppressing the error is a poor solution in this case. But, your advice is wrong. If the query succeeded and there were no results, then calling mysl_fetch_assoc() will simply return false - it would not produce an error. So, checking mysql_num_rows() would not apply in this scenario. The ONLY reason that mysl_fetch_assoc() is if the parameter (the result resource) is not a valid result resource. In 99% of cases this is because the query failed. But, in 100% of cases, if you used that same invalid parameter in a mysql_num_rows() call you would get the same error. So, your suggestion would cause the same error to occur, it would just happen on the mysql_num_rows() call rather than the mysl_fetch_assoc() call! You need to check if the query failed. //Ensure all values are integers and remove 0 values $prodIDs = array_filter(array_map('intval', $_SESSION['cart'])); //Create comma separated list $prodIDList = implode(', ', $prodIDs); $sql = "SELECT * FROM products WHERE id_products IN ($prodIDList) ORDER BY id_products ASC"; $result = mysql_query($sql); if(!$result) { //Query failed echo "Query: $sql<br>Error: " . mysql_error(); } elseif(mysql_num_rows($result)) { //Empty result set echo "No records found"; } else { //There were results while($row=mysql_fetch_assoc($query)) { //Output the data } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.