php:将我准备好的语句结果放入数组中

php:将我准备好的语句结果放入数组中

问题描述:

This is my code:

if ($stmt = $mysqli->prepare ( "SELECT album_no, album_name FROM $albumTable WHERE b_username=?  parent_name=? LIMIT 100" ))
        {

        $stmt->bind_param ( "ss", $a_owner,$p_name); // bind parameters for markers
        $stmt->execute ();
        $stmt->bind_result ($album_no,$album_name ); // bind result variables

        $stmt->fetch (); /* fetch value */

             if (isset ( $album_no ))
                {
                $array = array();
                $array['album_no']          = $album_no;
                $array['album_name']        = $album_name;
                }

        /* close statement */
        $stmt->close ();
        }

/* close connection */
$mysqli->close ();

and it works, the only problem is it returns just one row... I know I need a while() to go through the other rows but I just don't know where to put that while when using prepared statements :( searching the docs I find "fetchAll()" but that seems to only work with "PDO" and I have no idea what PDO is :(

Thanks in advance

This is probably what you're looking for:

$array = array();
while ($stmt->fetch()) 
{
    if (isset ($album_no))
    {
        $array['album_no'][]   = $album_no;
        $array['album_name'][] = $album_name;
    }
}

fetch() fetches results from a prepared statement into the bound variables. Once there are no more rows to be returned, fetch() returns NULL and the loop body will not be executed any more.

You need to fetch in a loop:

while($stmt->fetch()) {
    ... do stuff with your bound variables
}

Once there's no more rows available for fetching, the fetch() call will return a false and terminate the while() loop.