简体   繁体   English

如何在PHP网页上使用mySQL正确回显?

[英]How can I echo correctly using mySQL on a php web page?

I got a problem in querying an information in mySQL, here's the code: 我在查询mySQL中的信息时遇到了问题,这是代码:

SELECT avatar FROM amcms_users WHERE username='admin'

and the result is ' 59da6ceb5c74ac98f317a4b4af3c72f6.jpg ' which is correct. 结果是“ 59da6ceb5c74ac98f317a4b4af3c72f6.jpg ”是正确的。

Now when I load it on php page using these php codes... 现在,当我使用这些php代码将其加载到php页面上时...

<?php
$locAvatar = mysql_query("SELECT avatar FROM amcms_users WHERE username='admin'");
echo $locAvatar;
?>

and the result is wrong, ' Resource id #27 ' 结果是错误的,“ 资源ID#27

How can I echo it correctly? 如何正确回声? Thank you. 谢谢。

You need to fetch the data first, by using the mysql_fetch_* functions: 您需要先使用mysql_fetch_*函数获取数据:

$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) mysql_fetch_assoc读入数组)

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. mysql_query()返回的资源(对结果的引用mysql_query()将传递给mysql_fetch_系列函数之一。 These functions dereference the result and return the data in the appropriate format. 这些函数取消引用结果,并以适当的格式返回数据。

Have a look at: 看一下:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM