Jump to content

mysql_fetch_array


Saiv

Recommended Posts

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>
<?php
if(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 by Saiv
Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

  • 2 weeks later...

$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.

Link to comment
Share on other sites

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
    }
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.