duanhan9479 2011-07-01 09:50
浏览 38
已采纳

如何从我的mysqli准备查询结果中获取行对象数组

I would like to return all the results from my prepared query (mysqli) as objects in an array but i cant find a fetchall method or something similar. How do i go about this?

public function getImageResults ($search_term)
{
        if (empty($search_term))
            return false;

        $image_query = $this->db_connection->stmt_init();

        $image_query_sql =
        "
            SELECT 
                Images.url as url
            FROM 
                Images, ImageSearch, ImageSearchResults 
            WHERE 
                ImageSearch.search_string = ? AND
                ImageSearchResults.search_id = ImageSearch.id AND
                ImageSearchResults.image_id = Images.id AND

                Images.deleted = 0 AND
                ImageSearch.deleted = 0 AND
                ImageSearchResults.deleted = 0
        ";

        if ($image_query->prepare($image_query_sql))
        {
            $image_query->bind_param('s', $search_term);

            $image_query->execute();
            $image_query->store_result();
            $image_query->bind_result($url);

            return //need to return the entire result set here... any ideas?
        }

        return false;
}

I found this code on the net which kinda works but i get this error when i use it

Notice: Use of undefined constant mysqli_stmt_bind_result - assumed 'mysqli_stmt_bind_result'

private function getresult ($stmt)
    {
        $result = array();

        $metadata = $stmt->result_metadata();
        $fields = $metadata->fetch_fields();

        for (;;)
        {
            $pointers = array();
            $row = new stdClass();

            $pointers[] = $stmt;
            foreach ($fields as $field)
            {
                $fieldname = $field->name;
                $pointers[] = &$row->$fieldname;
            }

            call_user_func_array(mysqli_stmt_bind_result, $pointers);

            if (!$stmt->fetch())
                break;

            $result[] = $row;
        }

        $metadata->free();

        return $result;
    }
  • 写回答

2条回答 默认 最新

  • douxiong3234 2011-07-01 09:59
    关注

    The mysqli_stmt_bind_result on this line:

    call_user_func_array(mysqli_stmt_bind_result, $pointers);
    

    should be quoted:

    call_user_func_array('mysqli_stmt_bind_result', $pointers);
    

    You could also use PDO, an untested example but it should work:

    public function getImageResults ($search_term)
    {
        $sql =
        "
            SELECT 
                Images.url as url
            FROM 
                Images, ImageSearch, ImageSearchResults 
            WHERE 
                ImageSearch.search_string = ? AND
                ImageSearchResults.search_id = ImageSearch.id AND
                ImageSearchResults.image_id = Images.id AND
    
                Images.deleted = 0 AND
                ImageSearch.deleted = 0 AND
                ImageSearchResults.deleted = 0
        ";
        $pdo = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'username', 'password');
    
        $sth = $pdo->prepare($sql);
        $sth->execute(array($search_term));
    
        $result = $sth->fetchAll(PDO::FETCH_CLASS, "ArrayObject");
    
        //var_dump($result);//debug
    
        return $result;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图