只要存在 WHERE 语句,PHP MySQL 就会失败
我正在尝试对一个小型 MYSQL 表进行简单查询,但是当我插入Where子句时,我突然收到无效查询错误(在没有where的情况下添加了while(mysql_fetch_array){}
)。 MYSQL 控制台给出了 1064(语法)错误,但是,我检查了 MYSQL 文档,并且据我所知,我正在使用正确的语法。
<?php
$ind=rand(1,3);
$quote=Null;
$sign=Null;
$afil=Null;
$con=mysql_connect(localhost,root,********);//connect to database
mysql_select_db("phone_site",$con);//select table
$query="SELECT * FROM quotes WHERE index=$ind";//get the row for that index
$data=mysql_query($query);
//print out text
print ("<p id=\"quote\">" . $data['quote'] . "</p>");
print ("<p id=\"ename\">" . $data['sign'] . "</p>");
print ("<p id=\"afill\">-- " . $data['afil'] . "</p>");
mysql_close($con);//close connection
?>
有人知道问题是什么吗?我正在使用 XAMPP。是不是它的MYSQL有问题?
I am trying to make a simple query to a small MYSQL table, but when I insert the Where clause, I suddenly get an invalid query error (added a while(mysql_fetch_array ){}
when working without the where). The MYSQL console gives a 1064 (syntax) error, however, I checked the MYSQL documentation and I am using the proper syntax as far as I can determine.
<?php
$ind=rand(1,3);
$quote=Null;
$sign=Null;
$afil=Null;
$con=mysql_connect(localhost,root,********);//connect to database
mysql_select_db("phone_site",$con);//select table
$query="SELECT * FROM quotes WHERE index=$ind";//get the row for that index
$data=mysql_query($query);
//print out text
print ("<p id=\"quote\">" . $data['quote'] . "</p>");
print ("<p id=\"ename\">" . $data['sign'] . "</p>");
print ("<p id=\"afill\">-- " . $data['afil'] . "</p>");
mysql_close($con);//close connection
?>
Anyone know what the problem is? I'm using XAMPP. Is there something wrong with its MYSQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
INDEX
是MySQL的保留字之一:如果您有一列具有此类名称,则必须引用该列名称 - 请参阅 架构对象名称:
这意味着您的查询应如下所示:
为了完成我的答案,在看到 dustmachine 的一个,谁说:
我想该表是使用 phpMyAdmin 或 MySQL Workbench 等工具创建的;那些通常(对于某些人来说总是)引用列名,以避免此类问题。
评论后编辑:我没有看到,但你正在使用这种代码:
mysql_query 函数不会直接返回数据:它只返回一个“资源”(引用手册页面):
不确定它会解决您的所有问题(没有显示任何内容,甚至原始 HTML 标签也不奇怪),但它至少可能会有所帮助......
INDEX
is one of MySQL's Reserved Words :If you have a column with that kind of name, you must quote the column name -- see Schema Object Names :
Which means your query should look like this :
To complete my answer, after seeing dustmachine's one, who said :
I suppose the table has been created with some tool like phpMyAdmin or MySQL Workbench ; and those generaly (always, for some) quote column names, to avoid that kind of problem.
Edit after the comment : I didn't see, but you are using this kind of code :
The mysql_query function doesn't diretly return the data : it only returns a "ressource" (quoting the manual's page) :
Not sure it'll solve all your problems (nothing being displayed, not even the raw HTML tags is odd), but it might at least help a bit...
我认为问题在于
index
这个词的使用——这是一个 保留字具有特殊含义,不应用作列名。我有点惊讶的是,创建表时它没有抱怨。I think the problem is the use of the word
index
-- that's a reserved word with special meaning and shouldn't be used as a column name. I'm a little surprised that it didn't complain when the table was created.试试这个,它对我有用:
Try this, it works for me: