MySQL语法错误说"near'1'"但我的查询中没有'1' [英] MySQL syntax error says "near '1'" but I don't have a '1' in my query
问题描述
我第一次使用带有MySQL数据库的PHP创建网站,但遇到了我不理解的语法错误.我只想删除具有特定ID
的tblreparation
中的所有内容,以及具有相同ID
的rblrepstat
中的所有内容.我正在使用此代码:
I'm making a website for the first time using PHP with a MySQL database and getting a syntax error that I don't understand. I just want to delete everything from tblreparation
with a certain ID
and also everything from rblrepstat
with that same ID
. I am using this code:
<?php
$con=mysqli_connect("localhost","root","MYPASS","repair");
$ID = $_REQUEST['ID'];
$sql = mysqli_query($con, "DELETE FROM tblreparation WHERE ID = {$ID}");
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
$sql = mysqli_query($con, "DELETE FROM tblrepstat WHERE repID = {$ID}");
if (!mysqli_query($con,$sql)) {
die('Error2: ' . mysqli_error($con));
}
echo "1 record deleted";
mysqli_close($con);
?>
这是我得到的错误:
错误:您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本以使用正确的语法 在第1行的"1"附近
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
1附近?我什至看不到"1" ...
Near 1? I don't even see a '1'...
推荐答案
对于每个查询,您要两次调用mysqli_query.
You're calling mysqli_query twice for each query.
第二次调用它实际上是在传递资源作为查询参数,这会导致您得到错误.
The second time you call it you're actually passing a resource as the query parameter, which causes the error you're getting.
尝试将您的代码更改为此:
Try changing your code to this:
<?php
$con=mysqli_connect("localhost","root","MYPASS","repair");
$ID = $_REQUEST['ID'];
$sql = "DELETE FROM tblreparation WHERE ID = {$ID}";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
$sql = "DELETE FROM tblrepstat WHERE repID = {$ID}";
if (!mysqli_query($con,$sql)) {
die('Error2: ' . mysqli_error($con));
}
echo "1 record deleted";
mysqli_close($con);
?>
这篇关于MySQL语法错误说"near'1'"但我的查询中没有'1'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!