开发者

PHP prepared statement within a prepared statement

开发者 https://www.devze.com 2023-02-22 13:31 出处:网络
I\'m going through a video tutorial about doing a menu using a db. Instead of doing it with procedural PHP like in the video, I tried doing it with prepared statements OOP style. It doesn\'t work and

I'm going through a video tutorial about doing a menu using a db. Instead of doing it with procedural PHP like in the video, I tried doing it with prepared statements OOP style. It doesn't work and I can't figure out why.

It runs fine until line 17, where it dies with this error:

Fatal error: Call to a member function bind_param() on a non-object in C:\wamp\www\widget_corp\content.php on line 17

And here's the code:

    <?php
        $query = $connection->prepare('SELECT menu_name, id FROM subjects ORDER BY position ASC;');
        $query->execute();
        $query->bind_result($menu_name, $sid);

        while ($query->fetch()){
            echo "<li>{$menu_name} {$sid}</li>";

            $query2 开发者_JAVA技巧= $connection->prepare('SELECT menu_name FROM pages WHERE subject_id = ? ORDER BY position ASC;');
            $query2->bind_param("i", $sid); //This is line 17
            $query2->execute();
            $query2->bind_result($menu_name);
            echo "<ul class='pages'>";              
            while ($query2->fetch()){
                echo "<li>{$menu_name}</li>";
            }
            echo "</ul>";

        }
        $query->close();
    ?>

Is it impossible to do a prepared statement within stmt->fetch();?


Figured it out:

After executing and binding the result, it has to be stored (if another prepared statement is to be put in the fetch). So the fetching in this case has to be read from a buffered result.

In other words, can't execute another query until a fetch on the same connection is in progress.

The working code:

$query = $connection->prepare("SELECT menu_name, id FROM subjects ORDER BY position ASC;");
$query->execute();
$query->bind_result($menu_name, $sid);
$query->store_result();


$stmt = mysqli_prepare($con,"SELECT menu_name, id FROM subjects ORDER BY position ASC");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $menu_name, $id);
while (mysqli_stmt_fetch($stmt))
{
 $stmt2 = mysqli_prepare($con2,"SELECT menu_name FROM pages WHERE subject_id = ? ORDER BY position ASC;");
 mysqli_stmt_bind_param($stmt2,$id);
 mysqli_stmt_execute($stmt2);
 mysqli_stmt_bind_result($stmt2, $name);
 while (mysqli_stmt_fetch($stmt2))
  echo $name;
}

look at the $con and $con2, you can not execute a prepare statement within another ps using the same connection !!!


Yes, you can have several prepared statements : one of the ideas of prepared statements is "prepare once, execute several times".

  • So, you should prepare the statement outside of the loop -- so it's prepared only once
  • And execute it, several times, insidde the loop.


The Fatal error you get means that $query2 on line 17, is not an object -- which means the prepare failed.

A prepare typically fails when there is an error in it ; are you sure your query is valid ? The tables and columns names are OK ?

You should be able to get an error message, when the prepare fails, using mysqli->error() -- or PDO::errorInfo()


You don't say what DB extension you are using but you don't seem to test the return value of any function you are using. You can't assume that DB calls will always run flawlessly.

0

精彩评论

暂无评论...
验证码 换一张
取 消