mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in

8.8k Views Asked by At

I want to run multiple mysql queries( not concurrently). I am using prepared statements for doing so. This is a gist of my code :

    <?php



if(isset($_GET['username'])&&isset($_GET['activationid'])){

require_once("../database/db_connect.php");

$stmt= $mysqli->stmt_init();
$stmt->prepare("Select username FROM users where username= ? AND activationid= ?");
$username=$_GET['username'];
$activationid=$_GET['activationid'];
$stmt->bind_param("ss",$username,$activationid);
$stmt->execute();
$row=$stmt->get_result()->fetch_array(MYSQLI_ASSOC);

if(!strcmp($row['username'],$username)){
    echo 'you are registered successfully';



$stmt->prepare("UPDATE users SET active=yes where username = ?");
$stmt->bind_param("s",$username);
$stmt->execute();







}
}

?>

and db_connect.php is :

<?php 





define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','password');
define('DBNAME','Reminder');

    $mysqli= new mysqli(DBHOST,DBUSER,DBPASS,DBNAME) ;
    if($mysqli->connect_error) { 

    echo $mysqli->mysqli_connect_error();

    }
else {
echo "connected successfully";
}




?>

This gives me this error :

Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in

can anybody tell me what I am doing wrong ?

2

There are 2 best solutions below

3
On BEST ANSWER

Please chenge your code like below and check:-

//$stmt= $mysqli->stmt_init(); comment this line
$stmt = $mysqli->prepare("Select username FROM users where username= ? AND activationid= ?") or die( $mysqli->error);
$username=$_GET['username'];
$activationid=$_GET['activationid'];
$stmt->bind_param("ss",$userid,$activationid);
$stmt->execute();

And for second one same :-

$stmt = $mysqli->prepare("UPDATE users SET active=yes where username = ?") or die($mysqli->error);
$stmt->bind_param("s",$username);
$stmt->execute();

Note:- Please take care that your variables are properly defined and set. thanks.

1
On

after you prepare and after you bind, (and before execute), do a

echo $mysqli->error;

nothing like knowing what the heck is going on