How can I put the results of a MySQLi prepared statement into an associative array?

后端 未结 7 1494
清酒与你
清酒与你 2020-12-01 09:54

I have a sql query and a mysqli prepared statement:

$sql = \'SELECT photographers.photographer_id, photographers.photographer_name
    FROM photographers\';
         


        
7条回答
  •  甜味超标
    2020-12-01 10:28

    Update: Since PHP 5.3.0 you can get a mysqli_result object that provides a fetch_array method.

    $sql = 'SELECT photographers.photographer_id, photographers.photographer_name
        FROM photographers';
    $data = null;
    
    $stmt = $conn->stmt_init(); 
    if ($stmt->prepare($sql)) { 
        $stmt->bind_result($photographer_id, $photographer_name);  
        $OK = $stmt->execute();
        $result = $stmt->get_result();
        $data = $result->fetch_array();
    }
    

    Documentation: http://php.net/manual/en/mysqli-stmt.get-result.php

提交回复
热议问题