如何使用 MySQLI 多次绑定参数?
这就是我绑定参数的方式:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的方法不起作用,因为使用 mysqli_stmt_bind_param 的正确方法如下:
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:
refs: http://php.net/manual/en/mysqli-stmt.bind-param.php
to know the number of parameters makes a count() array.
MySQLi 的语句绑定确实不适合可变数量的参数。
我强烈建议切换到 PDO
MySQLi's statement binding really isn't suited to variable numbers of parameters.
I highly recommend switching to PDO