Jump to content


Photo

mysql_fetch_array

php

  • Please log in to reply
3 replies to this topic

#1 Saiv

Saiv

    Newbie

  • New Members
  • Pip
  • 8 posts

Posted 01 October 2013 - 12:17 AM

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, 01 October 2013 - 12:19 AM.


#2 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,861 posts
  • LocationCanada

Posted 01 October 2013 - 12:27 AM

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, 01 October 2013 - 12:32 AM.

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#3 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,329 posts

Posted 09 October 2013 - 04:13 AM

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



#4 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,861 posts
  • LocationCanada

Posted 09 October 2013 - 07:09 PM

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
    }
}

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com