简体   繁体   English

MySQL查询不显示结果

[英]mysql query not showing results

Newish to mysql. 新到mysql。 I have a query and it is not showing the value of the cell just the row name: 我有一个查询,它不仅仅显示行名而显示单元格的值:

$sql="SELECT 'first' from `users` WHERE `id`= $userid ";
$res=mysql_query($sql) or die(mysql_error());

$row=mysql_fetch_assoc($res); 

echo $row['first'] ; 

What am I doing wrong???? 我究竟做错了什么????

Brackets in your query is wrong: 您的查询中的括号是错误的:

$sql = "SELECT 'first' from `users` WHERE `id` = $userid";

Must be: 一定是:

$sql = "SELECT `first` from `users` WHERE `id` = $userid";

Note difference in first first注意差异

尝试:

echo $row[0]['first'];

First remove quotes from 'first' - it is a column so don't put it in quotes, you can use ` istead. 首先从“ first”中删除引号-这是一列,因此不要将其放在引号中,您可以使用`istead。 Next loop through results and that's all. 下一步遍历结果,仅此而已。

$sql="SELECT first from `users` WHERE `id`= $userid ";
$res=mysql_query($sql) or die(mysql_error());

while($row=mysql_fetch_assoc($res))
  echo $row['first'] ; 
SELECT 'first' 

will simply return the string first. 只会先返回字符串。

remove the quotes. 删除引号。

$sql="SELECT 'first' from users WHERE id = $userid "; $ sql =“ SELECT users “第一个”,其中id = $ userid”;

you are using normal quotes to select instead of backticks you are not selecting anything from the database. 您使用的是普通引号而不是反引号,您没有从数据库中选择任何内容。

use $sql="SELECT first from users WHERE id = $userid "; 使用SQL $ = “选择firstusers WHERE id = $用户ID”; instead 代替

and side note: never "be sure" that your query returns exactly 1 row 和侧面说明:永远不要“确定”查询返回的确切是1行

use mysql_fetch_assoc() in a loop and check if you really retrieve 1 result. 在循环中使用mysql_fetch_assoc()并检查您是否真的检索了1个结果。

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

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