Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an integer output from an SQL query

Tags:

sql

php

mysql

I have an SQL query as follows:

    $tagID = mysql_query("SELECT tagID FROM tags WHERE tagName = '$tag'");
     echo $tagID;

I want $tagID to contain something like 3, or 5, or any integer. But when I echo it, the output is:

resource id #4

How can I make it a simple integer?

like image 892
AKor Avatar asked Dec 16 '22 17:12

AKor


1 Answers

$result = mysql_query("SELECT tagID FROM tags WHERE tagName = '$tag'"); // figure out why an existing tag gets the ID zero instead of 'tagID'
$row = mysql_fetch_assoc($result);
echo $row["tagID"];

mysql_query() returns result resource, not the value in the query. You have to use fetch functions to get the actual data.

If you want this code to be cleaner, check that $result is not false (query error) and $row is not false (nothing found).

like image 136
StasM Avatar answered Dec 19 '22 08:12

StasM