douzhi8244 2014-01-21 05:46
浏览 58
已采纳

从MySQLi预处理语句获取一个关联或数字数组不工作

I can't getting an assoc or numeric array from a mysqli prepared statement. I searched and researched from code to get it but it have been imposible. I generate this code based on all that codes. I really think that's correct but it doesn't work anyway:

    $id = '134610';
    $sql = "SELECT * FROM table WHERE id = ?";
    $stmt = $mysqli->stmt_init();
    $stmt->prepare($sql);
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $result = $stmt->get_result();

            while ($array = $result->fetch_array(MYSQLI_ASSOC)) 
                               // or fetch_array(MYSQLI_NUM))
            print_r($array);
  • 写回答

2条回答 默认 最新

  • duancan1950 2014-02-06 00:32
    关注

    I finally resolve it with that code:

    $id = '134610';
    $sql = "SELECT * FROM table WHERE id = ?";
    $stmt = $mysqli->stmt_init();
    $stmt->prepare($sql);
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $array = self::bind_result_array($stmt);
    for($i=0;$stmt->fetch();$i++)
        $ArrayOfArrays[$i] = self::getCopy($array);
    $stmt->close();
    return $ArrayOfArrays;
    

    and this two functions:

    public function bind_result_array($stmt)
    {
    $meta = $stmt->result_metadata();
    $result = array();
    while ($field = $meta->fetch_field())
    {
        $result[$field->name] = NULL;
        $params[] = &$result[$field->name];
    }
    
    call_user_func_array(array($stmt, 'bind_result'), $params);
    return $result;
    }
    
    
    public function getCopy($row)
    {
    return array_map(create_function('$a', 'return $a;'), $row);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀