bindParam() 中的 Mysqli 准备语句不起作用
就像往常一样,我正在寻找 PHP 的最佳实践,准备好的语句似乎是我现在闭着眼睛应该如何做的东西。所以我开始尝试一些我发现的例子。
我在运行脚本时遇到此错误:
致命错误:调用成员函数 中的非对象上的bindParam() /opt/lampp/htdocs/phpSecurity/PreparedStatments/Insert-Multi-Binded-Params/Insert Simple Method.php 第 10 行
这是代码。
Insert Simple Method.php
<?php
require_once '../config.php';
$stmt = $db->prepare("INSERT INTO coisas (nome, telefone, bi) VALUES (?, ?, ?)");
$nome = 'Fabio Antunes';
$telefone = 916810641;
$bi = 123093456;
$stmt->bindParam(1, $nome);
$stmt->bindParam(2, $telefone);
$stmt->bindParam(3, $bi);
$stmt->execute();
$stmt->close();
$db->close();
?>
config.php
<?php
$server_host = 'localhost';
$server_user = 'root';
$server_password = '';
$server_db = 'PreparedStatements';
$db = new mysqli($server_host, $server_user, $server_password, $server_db);
?>
不确定我在这里做错了什么,这是在 php.net 上找到的类似示例,为什么不起作用? PS:我认为 mysqli 连接不是问题,因为我用它来使用 SELECT SQL 命令执行一些准备好的语句。并且工作得很好。
编辑
解决方案及其原因。
在 示例 中,我应该为每个使用 bind_param()
查询中的值。但多亏了巴特,他设法用我的代码解决了这个问题。
它在哪里:
$stmt->bindParam(1, $nome);
$stmt->bindParam(2, $telefone);
$stmt->bindParam(3, $bi);
应该是:
$stmt->bind_param("sii", $nome, $telefone, $bi);
现在对于那些可能想知道什么是“sii”的人来说。
好吧,据我所知,bind_param 将“$var”绑定到每个问号“?”为了。
因此,使用一个 bind_param()
我可以同时绑定它们,并且 bind_param()
的正常使用需要指定要绑定的数据类型。
我要绑定的第一个值是 $nome
一个字符串,由“s”指定;
其他的 $telefone
和 $bi
都是整数,因为他有“i”;
对于其他有类似问题的人来说,它是其他数据类型(来自 php.net)。
i = 整数;
s = 字符串;
d = 双精度;
b = 斑点;
如果有人有更好的解释,请发布或评论。所以我可以提高自己。
谢谢。
Just as usual i was looking around best practices with PHP, and prepared statements seems the kind of stuff i should now how do with my eyes closed. So i started playing around with some examples i've found.
I've got this error when running the script:
Fatal error: Call to a member function
bindParam() on a non-object in
/opt/lampp/htdocs/phpSecurity/PreparedStatments/Insert-Multi-Binded-Params/Insert
Simple Method.php on line 10
Here it goes the code.
Insert Simple Method.php
<?php
require_once '../config.php';
$stmt = $db->prepare("INSERT INTO coisas (nome, telefone, bi) VALUES (?, ?, ?)");
$nome = 'Fabio Antunes';
$telefone = 916810641;
$bi = 123093456;
$stmt->bindParam(1, $nome);
$stmt->bindParam(2, $telefone);
$stmt->bindParam(3, $bi);
$stmt->execute();
$stmt->close();
$db->close();
?>
config.php
<?php
$server_host = 'localhost';
$server_user = 'root';
$server_password = '';
$server_db = 'PreparedStatements';
$db = new mysqli($server_host, $server_user, $server_password, $server_db);
?>
Not sure what i'm doing wrong here, this is similar example found at php.net, why isn't working?
PS: I think the mysqli connection isn't the problem because I've used it to do some prepared statements with SELECT SQL commands. And worked pretty well.
EDIT
The Resolution and why.
Well in the example i should use bind_param()
for each value in the query. But thanks to Bart, he managed to solve the problem with my code.
Where it is:
$stmt->bindParam(1, $nome);
$stmt->bindParam(2, $telefone);
$stmt->bindParam(3, $bi);
It should be:
$stmt->bind_param("sii", $nome, $telefone, $bi);
Now for those who might wondering what is "sii".
Well bind_param for what i see it binds the "$var" to each question mark "?" in order.
So with one bind_param()
i can bind them all at the same time, and the normal use of bind_param()
requires to specify the type of data being binded.
My first value to be binded is $nome
a String, specified by the "s";
And the others $telefone
and $bi
are Integers for that he have "i";
For others that have a similar problem here it goes other data types (from php.net).
i = Integer;
s = String;
d = Double;
b = Blob;
If someone as a better explanation please post it or comment. So i can improve my own.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能认为连接没有任何问题,但您应该检查以确保:
编辑:
当您这样做时会发生什么:
?
表格
coisas
拼写正确吗?You may think there's nothing wrong with the connection, but you should check to make sure:
EDIT:
What happens when you do:
?
Is the table
coisas
spelled properly?在第 4 行返回 $stmt 后,对 $stmt 执行 print_r。它是一个真实的对象吗?我猜不会。
do a print_r on $stmt after you get it back on line 4. Is it a real object? I am guessing no.