How can I echo correctly using mySQL on a php web page?
I got a problem in querying an information in mySQL, here's th开发者_如何学编程e code:
SELECT avatar FROM amcms_users WHERE username='admin'
and the result is '59da6ceb5c74ac98f317a4b4af3c72f6.jpg' which is correct.
Now when I load it on php page using these php codes...
<?php
$locAvatar = mysql_query("SELECT avatar FROM amcms_users WHERE username='admin'");
echo $locAvatar;
?>
and the result is wrong, 'Resource id #27'
How can I echo it correctly? Thank you.
You need to fetch the data first, by using the mysql_fetch_*
functions:
$res = mysql_query("SELECT avatar FROM amcms_users WHERE username='admin'");
$locAvatar = mysql_fetch_assoc($res)["avatar"];
echo $locAvatar;
(mysql_fetch_assoc
fetches into an array)
You need to fetch the results and then echo them. The resource (which is a reference to the results) that mysql_query()
returns is to be passed on to one of the mysql_fetch_
series of functions. These functions dereference the result and return the data in the appropriate format.
Have a look at:
mysql_fetch_array
mysql_fetch_assoc
mysql_fetch_row
精彩评论