如何使用 MySQLI 多次绑定参数?

发布于 2024-12-13 01:55:46 字数 465 浏览 0 评论 0原文

这就是我绑定参数的方式:

$Con = mysqli_connect(...);
$Statement = mysqli_stmt_init($Con);

mysqli_stmt_prepare($Statement,"select * from users where name=? and email=?");
mysqli_stmt_bind_param("s",$Username);
mysqli_stmt_bind_param("s",$Email); <-- it fails here

但在其他情况下,当我将 2 个对 mysqli_stmt_bind_param 的调用替换为:

mysql_stmt_bind_param("ss",$Username,$Email)

问题是我有一个参数数组时,它工作得很好;我必须将它们一一绑定,因为我不知道参数的数量

This is how I'm binding my params:

$Con = mysqli_connect(...);
$Statement = mysqli_stmt_init($Con);

mysqli_stmt_prepare($Statement,"select * from users where name=? and email=?");
mysqli_stmt_bind_param("s",$Username);
mysqli_stmt_bind_param("s",$Email); <-- it fails here

But it works fine in the other case when I replace the 2 calls to mysqli_stmt_bind_param with:

mysql_stmt_bind_param("ss",$Username,$Email)

The problem is that I have an array of params; I have to bind them one by one coz I don't know the number of params

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

云仙小弟 2024-12-20 01:55:46

您的方法不起作用,因为使用 mysqli_stmt_bind_param 的正确方法如下:

mysql_stmt_bind_param("ss",$Username,$Email)

refs: http://php.net/manual/en/mysqli-stmt.bind-param.php

了解参数的数量构成 count() 数组。

Your approach does not work because the right way to use the mysqli_stmt_bind_param is precisely follow:

mysql_stmt_bind_param("ss",$Username,$Email)

refs: http://php.net/manual/en/mysqli-stmt.bind-param.php

to know the number of parameters makes a count() array.

情场扛把子 2024-12-20 01:55:46

MySQLi 的语句绑定确实不适合可变数量的参数。

我强烈建议切换到 PDO

$stmt = $pdo->prepare('select * from users where name=? and email=?');
$stmt->execute($numericArrayOfParameters);

MySQLi's statement binding really isn't suited to variable numbers of parameters.

I highly recommend switching to PDO

$stmt = $pdo->prepare('select * from users where name=? and email=?');
$stmt->execute($numericArrayOfParameters);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文