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

后端 未结 7 1476
清酒与你
清酒与你 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条回答
  •  Happy的楠姐
    2020-12-01 10:31

    I came across this discussion in order to find a solution for getting data from MySQLi prepared statements without the mysqlnd. I have been developing a class for handling prepared statements with MySQLi in a handy way. Please, take a look to the code, or simply use it (see an example of usage at the end of the piece of code) to fastly write prepared statements and get its results.

    class DbUtils {
    
        private $host;
        private $user;
        private $pass;
        private $database;
        private $connection;
    
        public function __construct($host, $user, $pass, $database) {
    
            $this->host = $host;
            $this->user = $user;
            $this->pass = $pass;
            $this->database = $database;
            $this->connection = new mysqli($host, $user, $pass, $database);
    
        }
    
        public function query(Array $params) {
    
            $args = array();
    
            // 0. Correct the input function parameters
            if (array_key_exists("query", $params)) {
                $args["query"] = $params["query"];
            } else {
                throw new Exception("Parameter not found: 'query'.");
            }
            if (array_key_exists("types", $params)) {
                $args["types"] = $params["types"];
            } else {
                $args["types"] = '';
            }
            if (array_key_exists("input", $params)) {
                $args["input"] = $params["input"];
            } else {
                $args["input"] = array();
            }
    
            // 1. Check the connection:
            if ($this->connection->connect_errno) {
                echo "Connection to MySQL failed: [" . $this->connection->connect_errno . "]: " . $this->connection->connect_error . "
    "; } // 2. Prepare the sentence: if (!($stmt = $this->connection->prepare($args["query"]))) { echo "Prepared statement failed: [" . $stmt->errno . "]: " . $stmt->error . "
    "; } // 3. Bind the input parameters: if ( ( 0 != sizeof( $args["input"] ) ) && !(call_user_method_array("bind_param", $stmt, array_merge(array($args["types"]), $args["input"])))) { echo "Binding parameters failed: [" . $stmt->errno . "]: " . $stmt->error . "
    "; } // 4. Execute the sentence if (!($stmt->execute())) { echo "Sentence execution failed: [" . $stmt->errno . "]: " . $stmt->error . "
    "; } // 5. Bind the results: $data = array(); $meta = $stmt->result_metadata(); $row = array(); while( $field = $meta->fetch_field() ) { $argos[] = &$row[$field->name]; } call_user_method_array('bind_result', $stmt, $argos); // 6. Collect the results: while ($stmt->fetch()) { foreach($argos as $key => $val) { $dataItem[$key] = $val; } $data[] = $dataItem; } // 7. Close the sentence: $stmt->close(); // 8. Return interesting data properly ordered: return $data; } } // 1. Instantiate it: $dbUtils = new DbUtils( "127.0.0.1", "user", "password", "database" ); // 2. Query prepared statements like this: $users = $dbUtils->query(array( "query" => "SELECT * FROM user WHERE name LIKE ? AND pass LIKE ?;", "input" => array('%', '%'), "types" => 'ss' )); // 3. Enjoy securely CRUD Ops!

提交回复
热议问题