php SQL 语法错误 MariaDB 服务器版本,用于在第 1 行的“WHERE ID = 4”附近使用正确的语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27302605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:17:55  来源:igfitidea点击:

SQL syntax error MariaDB server version for the right syntax to use near 'WHERE ID = 4' at line 1

phpmysqlsqlmysqli

提问by Sep

I have this line of SQL:

我有这行 SQL:

$sql = "SELECT ID, ListStID, ListEmail, Title FROM $entry_database WHERE ID = '". $ReqBookID ."'";
$result = mysqli_query($conn, $sql);

As you can see, I am selecting an entry's ID, ListStID, ListEmail and Title Column if ID is equal to a string of numbers (or text), which is given by user in a form.

如您所见,如果 ID 等于用户在表单中给出的一串数字(或文本),我将选择条目的 ID、ListStID、ListEmail 和标题列。

Everything is ok, and I don't get any syntax error when I write the code (I am using a code editor software. However, when I use it online, I get this error:

一切正常,我写代码时没有出现任何语法错误(我使用的是代码编辑器软件。但是,当我在线使用它时,我得到了这个错误:

Error: SELECT ID, ListStID, ListEmail, Title FROM WHERE ID = '4' You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE ID = '4'' at line 1

错误:SELECT ID, ListStID, ListEmail, Title FROM WHERE ID = '4' 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,了解在第 1 行的“WHERE ID = '4'”附近使用的正确语法

I am very new to PHP, and I'm sure I am either adding extra ' or ", so I would really appreciate it if you could help me with this issue. I have tried the answers for similar questions, but no success yet.

我对 PHP 很陌生,我确定我要添加额外的 ' 或 ",所以如果你能帮助我解决这个问题,我将不胜感激。我已经尝试过类似问题的答案,但还没有成功。

回答by Forien

You have empty $entry_databasevariable. As you see in error: ListEmail, Title FROM WHERE IDbewteen FROMand WHEREshould be name of table. Proper syntax of SELECT:

你有空$entry_database变量。正如你在错误看到:ListEmail, Title FROM WHERE IDbewteen FROMWHERE应该是表的名称。SELECT 的正确语法:

SELECT columns FROM table [optional things as WHERE/ORDER/GROUP/JOIN etc]

which in your way should become:

以您的方式应该成为:

SELECT ID, ListStID, ListEmail, Title FROM some_table_you_got WHERE ID = '4'

回答by Tim Lewis

You're missing your database name:

您缺少数据库名称:

$sql = "SELECT ID, ListStID, ListEmail, Title FROM ".$entry_database." WHERE ID = ". $ReqBookID .";

And make sure that $entry_database isn't null or empty:

并确保 $entry_database 不为 null 或为空:

var_dump($entry_database);

Also notice that you don't need to have $ReqBookID in '' as if it's an Int.

另请注意,您不需要将 $ReqBookID 放在 '' 中,就好像它是 Int 一样。

回答by Sundeep Singh

This problem also arise when we don't give the single or double quotes to the database value.

当我们不给数据库值提供单引号或双引号时,也会出现这个问题。

Wrong way:

错误的方法:

$query ="INSERT INTO tabel_name VALUE ($value1,$value2)";

As database inserting values must be in quotes ' '/" "

由于数据库插入值必须用引号' '/" "

Right way:

正确的方法:

$query ="INSERT INTO STUDENT VALUE ('$roll_no','$name','$class')";