Hardik Dave
Hardik Dave

Reputation: 706

mysql_query returning value as 0

I have a query:

$img_id = mysql_query("SELECT img_id FROM img_path where ul_id = $ul_id ");     
$img_id = $img_id[0];

$img_id at end is output as 0, I want the value that is stored for img_id in the table img_path.

I have checked the database connections and they are fine. Any help?

Upvotes: 0

Views: 69

Answers (3)

citizen404
citizen404

Reputation: 1515

Your $img_id contains a result resource that is returned by mysql_query.

To get the query result you have to fetch it with one of the mysql_fetch_* functions, mysql_fetch_array, i.e.

Also, for security reasons you should always escape your variables passed to the query.

But as mysql is deprecated you should better use mysqli or pdo

Anyway, this is how you do it with mysql:

$ul_id = mysql_real_escape_string($ul_id);
$img_id = mysql_query("SELECT img_id FROM img_path where ul_id = $ul_id ");
$row = mysql_fetch_array($img_id)
$img_id = $row['img_id']; // access through associative indices or
// $img_id = $row[0]; // access through number indices

Upvotes: 1

echo_Me
echo_Me

Reputation: 37233

try this:

$ul_id = mysql_real_escape_string($ul_id);
$img_id =mysql_query("SELECT img_id FROM img_path where ul_id = '".$ul_id."' "); 
$row = mysql_fetch_array($img_id);
$img = $row['img_id'];

or if you looking for fetch_row

then use this:

$ul_id = mysql_real_escape_string($ul_id);
$img_id =mysql_query("SELECT img_id FROM img_path where ul_id = '".$ul_id."' "); 
$row = mysql_fetch_row($img_id);
$img = $row[0];

  • consider to change to MYSQLI or PDO as mysql is deprecated.

  • consider to escape your variables.

Upvotes: 0

Andolasoft Inc
Andolasoft Inc

Reputation: 1296

Please try with this code. You need to fetch the record

$img_id = mysql_query("SELECT img_id FROM img_path where ul_id = $ul_id "); 

$imgID = mysql_fetch_array($img_id);

$img_id1 = $imgID['img_id'];

Upvotes: 0

Related Questions