就像往常一样,我正在四处寻找PHP的最佳实践,并且准备好的语句似乎是我现在应该如何闭上眼睛的东西。所以我开始玩弄一些我发现的例子。mysqli Prepared Statement in bindParam()不起作用
运行脚本时,我得到这个错误:
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
这里是不言而喻的代码。
插入简单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命令做一些准备好的语句。并且工作得很好。
编辑
分辨率和原因。
那么在example我应该使用bind_param()
为查询中的每个值。但是,感谢Bart,他设法用我的代码解决了这个问题。
在哪里,它是:
$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
是整数,因为他有“我”;
对于其他有类似问题的人来说,这里还有其他数据类型(来自php.net)。
i = Integer;
s = String;
d = Double;
b = Blob;
如果有人作为更好的解释请发布或评论。所以我可以改进我自己的。
谢谢。
良好的最佳实践建议。我试过了,但没有解决问题。任何其他想法? – 2009-11-04 21:36:47
$ stmt-> bind_param(“sii”,$ nome,$ telefone,$ bi);工作。 – 2009-11-04 21:45:22
很高兴听到它法比欧。 – 2009-11-04 21:46:23