How to get an integer output from an SQL query?

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

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

1 sql injection vulnerable solution gets accepted and upvoted. Why I am not surprised? – Col.

Shrapnel Feb 16 at 18:42 @Col. Shrapnel you don't have to fit full comp. Sci and security course in every quick fix.

The question was about specific query, and the answer was too. How $tag is filtered is entirely different topic which wasn't discussed here. – StasM Feb 17 at 22:15.

It's always a shock to see not a single programmer in the answers. I know the OP is not a programmer too, so, my answer would be totally in vain but what the heck. Here is a example of a thing called a function: $val) { $args$key = "'".

Mysql_real_escape_string($val). "'"; } $query = vsprintf($query, $args); $res = mysql_query($query); if (!$res) { trigger_error("dbgetarr: ". Mysql_error()." in ".

$query); return FALSE; } else { $row = mysql_fetch_row($res); if (!$row) return NULL; return $row0; } } this code can be saved in some configuration file and then called in this manner: $tagID = dbgetvar("SELECT tagID FROM tags WHERE tagName = %s",$tag); echo $tagID.

The mysql_query function, by itself, returns a 'resource' on success and false on error. In this case, you're getting a resource that has id #44, which is what you might expect from that function. What you could do is take the result of mysql_query and use mysql_fetch_assoc to convert the resource to an associative array.(Also check out mysql_fetch_row, or mysql_fetch_field for other techniques).

Here's a typical way of structuring this problem: $query = "SELECT tagID FROM tags WHERE tagName = '$tag'"; $result = mysql_query($query); $array = mysql_fetch_assoc($result); $tagID = $array'tagID'; //your integer. Please see the mysql_query PHP Manual entry for more info. Check out the user comments at the bottom for particularly good advice and sample code.

You're missing a single step. Try this: $resource = mysql_query("SELECT tagID FROM tags WHERE tagName = '$tag'"); $tagID = mysql_fetch_assoc($resource); print_r($tag_id); If your query returns more than one row (i.e. There is more than one tag with the same tagName), you'll want to put it in a loop: $resource = mysql_query("SELECT tagID FROM tags WHERE tagName = '$tag'"); while($tagID = mysql_fetch_assoc($resource)) { echo $tagID'tagID'; } Addendum Although the above code will solve your problem, I urge you to stop right there and learn about mysqli instead.It's a much newer, more robust solution than using the mysql_* functions.

From the docs: The mysqli extension, or as it is sometimes known, the MySQL improved extension, was developed to take advantage of new features found in MySQL systems versions 4.1.3 and newer. The mysqli extension is included with PHP versions 5 and later. The mysqli extension has a number of benefits, the key enhancements over the mysql extension being: Object-oriented interface Support for Prepared Statements Support for Multiple Statements Support for Transactions Enhanced debugging capabilities Embedded server support Also from the docs: If you are using MySQL versions 4.1.3 or later it is strongly recommended that you use the MySQLi extension.

Mysqli is not that big deal. When properly used, mysql can do everything you mentioned here. – Col.

Shrapnel Feb 16 at 18:53.

A SQL query always returns a SQL Resource result, an arguably unreadable object that contains the results of the query. Because of the way that databases are stored, the way that users may want to manipulate data, and the sheer amount of data, it's easier to store it as an identifier than as an object. To get the data, you need, you must first convert it to an array: $result = mysql_query("SELECT tagID FROM tags WHERE tagName = '$tag'"); $row = mysql_fetch_assoc($result); echo $row"tagID"; (Where $rowcolumn is the column you want to pull data from( Or an object: $result = mysql_query("SELECT tagID FROM tags WHERE tagName = '$tag'"); $object = mysql_fetch_object($result); echo $object->tagID; (where $object->column is the column you want to pull data from) Hope this helps.

Php - How to get an integer output from an SQL query - Stack Overflow.

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

It's always a shock to see not a single programmer in the answers. I know the OP is not a programmer too, so, my answer would be totally in vain but what the heck.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions