duangu4943 2014-09-09 16:35
浏览 27
已采纳

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

  • 写回答

2条回答 默认 最新

  • ds08541 2014-09-09 16:38
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动