简体   繁体   English

mysqli prepare返回false,但SQL有效

[英]mysqli prepare returns false but SQL is valid

I'm using the prepare function from mysqli to create a query to my database but it fails on me even when I have many request working earlier in the same page. 我正在使用mysqli的prepare函数来创建对数据库的查询,但是即使我在同一页面的较早位置上有很多请求,它也对我失败。

My query looks like this : 我的查询如下所示:

if ($insert = $mysqli->prepare( "INSERT INTO table_name (id_user, token) VALUES (?, ?)")) {
    // Do some cool stuff here
} else {
    var_dump($mysqli->error);
    var_dump($insert->error);
}

During the execution, it goes into the else and the output is : 在执行过程中,它进入else,输出为:

string(0) "" //var_dump($mysqli->error);
Notice: Trying to get property of non-object in // var_dump($insert->error);

I know that my $mysqli is working so I also tested out these strings inside the prepare statement. 我知道我的$ mysqli正在工作,因此我也在prepare语句中测试了这些字符串。 None of them worked ether. 他们都没有以太工作。 (I tested them in mysql/phpmyadmin and they work fine) (我在mysql / phpmyadmin中测试了它们,它们工作正常)

"INSERT INTO `table_name` (`id_user`, `token`) VALUES (?, ?)"
"INSERT INTO `table_name` (`table_name`.`id_user`, `table_name`.`token`) VALUES (?, ?)"
'INSERT INTO `table_name` (`id_user`, `token`) VALUES (50, "random_token")'

Can someone help me with this little headache? 有人可以帮我这个小小的头痛吗?

Thanks 谢谢

The error is actually complaining about $insert not being defined... Which makes sense, as it's hitting your else statement. 该错误实际上是在抱怨未定义$ insert ...这很有意义,因为它正在击中您的else语句。

Notice: Trying to get property of non-object in // var_dump($insert->error);

Notice the error you yourself commented, and what it's actually complaining about. 请注意您自己注释的错误以及它实际上在抱怨什么。 You shouldn't be referencing the $insert variable in the else statement 您不应在else语句中引用$ insert变量

I finally figured it out. 我终于弄明白了。 The problem is that I didn't closed my previous connections. 问题是我没有关闭以前的连接。 One of them was already using the same name $insert 其中一个已经在使用相同的名称$ insert

Two possible fixes I found for someone who could find this thread. 我为可能找到此线程的人找到了两个可能的修复程序。

Fix 1 - CLOSE YOUR CONNECTIONS , it's as simple has 修复1- 关闭您的连接 ,就这么简单

$connectionVariable->close();

Fix 2 - Name your variables differently. 修正2-用不同的名称命名变量。 Although you should always do the Fix 1 first. 虽然您应该始终先执行Fix 1。 I guess I had to find it the hard way. 我想我必须很难找到它。

PS Thanks to everyone who helped me PS感谢所有帮助我的人

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

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