只要存在 WHERE 语句,PHP MySQL 就会失败

发布于 2024-08-13 17:29:46 字数 789 浏览 9 评论 0原文

我正在尝试对一个小型 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半世蒼涼 2024-08-20 17:29:46

INDEX是MySQL的保留字之一

某些单词,例如 SELECT、DELETE、
或 BIGINT 被保留并需要
特殊处理用作
表和列等标识符
名称。这也可能适用于
内置函数的名称。

允许保留字
标识符,如果您将它们引用为
第 8.2 节“模式
对象名称”

如果您有一列具有此类名称,则必须引用该列名称 - 请参阅 架构对象名称

标识符引号字符是
反引号(“`”)

这意味着您的查询应如下所示:

$query="SELECT * FROM quotes WHERE `index`=$ind";

为了完成我的答案,在看到 dustmachine 的一个,谁说:

我有点惊讶它没有
创建表时抱怨。

我想该表是使用 phpMyAdmin 或 MySQL Workbench 等工具创建的;那些通常(对于某些人来说总是)引用列名,以避免此类问题。

评论后编辑:我没有看到,但你正在使用这种代码:

$data=mysql_query($query);
print ("<p id=\"quote\">" . $data['quote'] . "</p>");

mysql_query 函数不会直接返回数据:它只返回一个“资源”(引用手册页面)

返回的结果资源应该是
传递给 mysql_fetch_array()
和其他处理函数
结果表,访问返回的
数据。

不确定它会解决您的所有问题(没有显示任何内容,甚至原始 HTML 标签也不奇怪),但它至少可能会有所帮助......

INDEX is one of MySQL's Reserved Words :

Certain words such as SELECT, DELETE,
or BIGINT are reserved and require
special treatment for use as
identifiers such as table and column
names. This may also be true for the
names of built-in functions.

Reserved words are permitted as
identifiers if you quote them as
described in Section 8.2, “Schema
Object Names”

If you have a column with that kind of name, you must quote the column name -- see Schema Object Names :

The identifier quote character is the
backtick (“`”)

Which means your query should look like this :

$query="SELECT * FROM quotes WHERE `index`=$ind";

To complete my answer, after seeing dustmachine's one, who said :

I'm a little surprised that it didn't
complain when the table was created.

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 :

$data=mysql_query($query);
print ("<p id=\"quote\">" . $data['quote'] . "</p>");

The mysql_query function doesn't diretly return the data : it only returns a "ressource" (quoting the manual's page) :

The returned result resource should be
passed to mysql_fetch_array(),
and other functions for dealing with
result tables, to access the returned
data.

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...

鸠书 2024-08-20 17:29:46

我认为问题在于 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.

夜灵血窟げ 2024-08-20 17:29:46

试试这个,它对我有用:

$query="SELECT * FROM quotes WHERE index='$ind'";

Try this, it works for me:

$query="SELECT * FROM quotes WHERE index='$ind'";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文