bindParam() 中的 Mysqli 准备语句不起作用

发布于 2024-08-10 06:00:41 字数 2064 浏览 3 评论 0原文

就像往常一样,我正在寻找 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦毁影碎の 2024-08-17 06:00:42

您可能认为连接没有任何问题,但您应该检查以确保:

$db = new mysqli($server_host, $server_user, $server_password, $server_db);
if (mysqli_connect_errno()) {
    printf("DB error: %s", mysqli_connect_error());
    exit();
}

编辑:

当您这样做时会发生什么:

$stmt = $db->prepare("INSERT INTO coisas (nome, telefone, bi) VALUES (?, ?, ?)");
$stmt->bind_param("sii", $nome, $telefone, $bi);
$stmt->execute();

表格 coisas 拼写正确吗?

You may think there's nothing wrong with the connection, but you should check to make sure:

$db = new mysqli($server_host, $server_user, $server_password, $server_db);
if (mysqli_connect_errno()) {
    printf("DB error: %s", mysqli_connect_error());
    exit();
}

EDIT:

What happens when you do:

$stmt = $db->prepare("INSERT INTO coisas (nome, telefone, bi) VALUES (?, ?, ?)");
$stmt->bind_param("sii", $nome, $telefone, $bi);
$stmt->execute();

?

Is the table coisas spelled properly?

白衬杉格子梦 2024-08-17 06:00:42

在第 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文