How to bind a parameter several times with MySQLI?

This is how I bind my parameters:

$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 another case, when I replace 2 calls of mysqli_stmt_bind_param with:

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

The problem is that I have an array of parameters; I have to knit them one by one. I do not know the number of parameters

+4
source share
3 answers

A bit offtopic, but I find this important enough.

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

You see, this site, although encouraged by laziness, does not always answer your question better than the good old Google and manual ones can.

-1
source

Your approach does not work, because the correct way to use mysqli_stmt_bind_param is followed exactly:

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

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

to find out how many parameters the count () array does.

+5
source

Binding MySQLi statements is really not suitable for variable parameter numbers.

I highly recommend switching to PDO

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

Source: https://habr.com/ru/post/1379298/


All Articles