-1

I normally read the data from the database, but I get an error when using the while.

My code is:

$BarberId    = 1;
$stmt = $db->prepare("CALL `GetBranch`(?);");
$stmt->bind_param('i', $BarberId);
$stmt->execute();

$Tree_Barber_Id = NULL;
$stmt->bind_result($Tree_Barber_Id);
$stmt->store_result();

if($stmt->num_rows)
{
    while($stmt->fetch())
    {        
        $Priod = NULL;
        $stmt2 = $db->prepare("SELECT `priod` FROM `t_barber` WHERE `id`=?");

        $stmt2->bind_param('i', $Tree_Barber_Id); //ERROR IS HERE!!!

        $stmt2->execute();
        $stmt2->bind_result($Priod);
        $stmt2->store_result();
    }
}

$stmt->close();

I think the error was caused because the variable stmt has not been closed yet. But by closing that by $stmt->close(); while command will not work.

Error is :

Fatal error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\file1.php on line 17
javad
  • 833
  • 14
  • 37
  • If you use PDO, I'm not sure if method bind_param() exists. Try wit bindParam() method. Ref : [https://secure.php.net/manual/en/pdostatement.bindparam.php](https://secure.php.net/manual/en/pdostatement.bindparam.php) – anggriyulio Feb 10 '19 at 18:04
  • @anggriyulio I'm use mysqli, line 3 is work – javad Feb 10 '19 at 18:14
  • 1
    You could fetch all of the ID's from the procedure and then use `IN` in your select to fetch all of the ID's in 1 statement (https://stackoverflow.com/questions/17870999/bind-multiple-parameters-into-mysqli-query) – Nigel Ren Feb 10 '19 at 18:16

1 Answers1

2

Read the error message.

Fatal error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\file1.php on line 17

Line 17 is this:

$stmt2->bind_param('i', $Tree_Barber_Id);

The error message is telling you $stmt2 is a boolean. $stmt2 comes from this line:

$stmt2 = $db->prepare("SELECT `priod` FROM `t_barber` WHERE `id`=?");

mysqli's prepare() function returns false if there was an error.

This means your query is invalid. You can find out what's invalid about it by looking at $db->error

rickdenhaan
  • 10,857
  • 28
  • 37
  • The command is executed directly in the MySql. $db->error is "Commands out of sync; you can't run this command now" – javad Feb 10 '19 at 18:09
  • Ok, maybe the answers in [this question](https://stackoverflow.com/q/614671/1941241) can point you in the right direction to fix that issue – rickdenhaan Feb 10 '19 at 18:15
  • In fact, I have to use the following command after using the stored procedure command:$db->next_result(); – javad Mar 25 '19 at 05:29