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

后端 未结 7 1378
清酒与你
清酒与你 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:46

    If you cannot use the PDO extension. Or you are having trouble building your database class with prepared statements. How to use for insert update, delete and insert:

        $db = new database();
        $db->query = "INSERT INTO blabla (name,date,number) VALUES(?,?,?)";
        $db->params = array($name,$date,$number);
        $db->type = 'ssi'; //s=string,i=integer
        if($db->insert())
            echo 'success';
    

    Fetch works a bit different

        $array = array();
        $db = new database();
        $db->query = "SELECT * FROM blabla WHERE id=? and someother=?";
        $db->params = array($id,$other);
        $db->type = 'is';
        $r = $db->fetch(); 
        //$r[0]['id'] for row 1
        //$r[0]['name'] for row 1
        //$r[1] .... For row 2
        //$r[2] .... For row 3
        //etc...
    

    Now for the database class

        class database {
    
            private $stmt;
            private $mysqli;
            private $query;
            private $params = array();
            private $type;
    
            public function __set($name, $value) {
                switch ($name) {
                    case 'params':
                        $this->params = $value;
                        break;
                    case 'query':
                        $this->query = $value;
                        break;
                    case 'type':
                        $this->type = $value;
                        break;
                    default:
                        break;
                }
            }
    
            public function __get($name) {
                if ($name !== "mysqli" && $name !== "stmt")
                    return $this->$name;
            }
    
            public function __construct() {
                $this->mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT);
                $this->stmt = $this->mysqli->stmt_init();
            }
    
            private function close_con($bool) {
                if ($bool) {
                    $this->stmt->free_result();
                }
                $this->stmt->close();
                $this->mysqli->close();
            }
    
            private function nofetch() {
                $this->stmt->prepare($this->query);
                $bind_names[] = $this->type;
                for ($i = 0; $i < count($this->params); $i++) {
                    $bind_name = 'bind' . $i;
                    $$bind_name = $this->params[$i];
                    $bind_names[] = &$$bind_name;
                }
                call_user_func_array(array($this->stmt, "bind_param"), $bind_names);
    
                if ($this->stmt->execute()) {
    
                    $this->close_con(false);
                    return true;
                }
                $this->close_con(false);
                return false;
            }
    
            public function insert() {
                if ($this->nofetch()) {
                    return true;
                }
                return false;
            }
    
            public function update() {
                if ($this->nofetch()) {
                    return true;
                }
                return false;
            }
    
            public function delete() {
                if ($this->nofetch()) {
                    return true;
                }
                return false;
            }
    
            public function fetch() {
                $result_out = array();
                $this->stmt->prepare($this->query);
                $bind_names[] = $this->type;
                if (count($this->params) > 0) {
                    for ($i = 0; $i < count($this->params); $i++) {
                        $bind_name = 'bind' . $i;
                        $$bind_name = $this->params[$i];
                        $bind_names[] = &$$bind_name;
                    }
                    call_user_func_array(array($this->stmt, "bind_param"), $bind_names);
                }
                if ($this->stmt->execute()) {
                    $result = $this->stmt->result_metadata();
                    $cols = $result->fetch_fields();
                    foreach ($cols as $col) {
    
                        $name = str_replace("-", "_", $col->name);
    
                        $$name = null;
                        if ($name == null)
                            $name = 'name';
                        $bindarray[$name] = &$$name;
                    }
    
                    call_user_func_array(array($this->stmt, 'bind_result'), $bindarray);
                    $this->stmt->store_result();
                    $copy = create_function('$a', 'return $a;');
                    while ($this->stmt->fetch()) {
                        $result_out[] = array_map($copy, $bindarray);
                    }
                }
                $this->close_con(true);
                return $result_out;
            }
    
        }
    

    I hope this is helpful

    0 讨论(0)
提交回复
热议问题