I have an SQL query as follows:
$tagID = mys开发者_如何学Goql_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?
$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).
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:
<?
function dbgetvar(){
$args = func_get_args();
$query = array_shift($args);
foreach ($args as $key => $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 $row[0];
}
}
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.
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 $row[column] 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.
精彩评论