Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind param multiple times with MySQLI?

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

like image 954
jondinham Avatar asked Nov 02 '11 12:11

jondinham


2 Answers

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.

like image 161
JellyBelly Avatar answered Nov 06 '22 01:11

JellyBelly


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);
like image 44
Phil Avatar answered Nov 06 '22 00:11

Phil