简体   繁体   English

简单的MySQL查询无法在PHP中工作

[英]Simple mysql query not working in php

Suddenly the sql query is not woking in add table. 突然,SQL查询未在添加表中唤醒。

It works in other table though. 它可以在其他表中使用。 Is there any wrong in code? 代码有什么错误吗?

$sql="select index,name from add Limit 0,10";
$result=mysql_query($sql);
    while($row=mysql_fetch_assoc($result))
    { 
echo $row['name'];
}

The index name is filled with random data. 索引名称填充有随机数据。

add is a reserved word in MySQL. add是MySQL中的保留字 You have to quote it. 您必须引用它。

Try this: 尝试这个:

$sql="select index,name from `add` Limit 0,10";
$result=mysql_query($sql);
while($row=mysql_fetch_assoc($result))
{ 
   echo $row['name'];
}

Backticks around the table name. 表格名称周围的反引号。 'add' is a keyword “ add”是关键字

$sql="select index,name from add Limit 0,10";

add is a reserved keyword in MySQL. add是MySQL中的保留关键字。 So is index . index也是如此。 Escape these two using backticks and it shall work: 使用反引号转义这两个,它将起作用:

$sql="select `index`,name from `add` Limit 0,10";
$sql="select `index`,`name` from `add` Limit 0,10";
$result=mysql_query($sql);
    while($row=mysql_fetch_assoc($result))
    { 
echo $row['name'];
}

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

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