How to bind param multiple times with MySQLI?

孤街浪徒 提交于 2019-12-22 06:49:04

问题


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


回答1:


A bit offtopic but I find it important enough.

A very recent user comment in the manual page for mysql_stmt_bind_param contains the exact answer to this very question.

You see, this site, although encourage laziness, not always answer your question better than good old google and manual can.




回答2:


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.




回答3:


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);


来源:https://stackoverflow.com/questions/7980140/how-to-bind-param-multiple-times-with-mysqli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!