0

Whenever I run this code, I get a resource id. How do I fix this:

function ct()
{
    $result = mysql_query("SELECT `Quantity` FROM `shopping cart` WHERE `Customer_id`=1") or die(mysql_error());
    mysql_fetch_array($result);   
    echo "$result";
}
r5d
  • 579
  • 5
  • 24

2 Answers2

4

You need to fetch result by mysql_fetch_array or mysql_fetch_assoc

$row = mysql_fetch_array($result);

and

what mysql_query return is

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.


After edit in question

This mysql_fetch_array($result); Returns an array that corresponds to the fetched row and moves the internal data pointer ahead. So you must assigned that array to any variable to use like

while($row = mysql_fetch_array($result)){   
 echo $row ['field_name'];
}

Or

$row = mysql_fetch_array($result); // want to fetch only one row 
 echo $row ['field_name'];
Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
-1
$link = mysqli_connect("HOSTNAME", "USERNAME", "PASS", "DBNAME");    
$result = mysqli_query($link,"SELECT `Quantity` FROM `shopping cart` WHERE `Customer_id`=1") or die(mysql_error());
while($row = mysqli_fetch_array($result))
{
    echo $row['Quantity'];
    echo "<br />";
}
mysqli_close($con);
mrjimoy_05
  • 3,452
  • 9
  • 58
  • 95
Praveen kalal
  • 2,148
  • 4
  • 19
  • 33