note 57943 modified in function.mysqli-stmt-bind-result by danbrown

From: Date: Fri, 28 Nov 2008 22:36:11 +0000
Subject: note 57943 modified in function.mysqli-stmt-bind-result by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147363@lists.php.net to get a copy of this message
I've created these functions which will act like mysqli_fetch_array() and mysqli_fetch_object() but work with bound results. <?php function fetch_object() { $data = mysqli_stmt_result_metadata($this->stmt); $count = 1; //start the count from 1. First value has to be a reference to stmt. $fieldnames[0] = &$this->stmt; $obj = new stdClass; while ($field = mysqli_fetch_field($data)) { $fn = $field->name; //get all the feild names $fieldnames[$count] = &$obj->$fn; //load the fieldnames into an object.. $count++; } call_user_func_array(mysqli_stmt_bind_result, $fieldnames); mysqli_stmt_fetch($this->stmt); return $obj; } function fetch_array() { $data = mysqli_stmt_result_metadata($this->stmt); $count = 1; //start the count from 1. First value has to be a reference to the stmt. because bind_param requires the link to $stmt as the first param. $fieldnames[0] = &$this->stmt; while ($field = mysqli_fetch_field($data)) { $fieldnames[$count] = &$array[$field->name]; //load the fieldnames into an array. $count++; } call_user_func_array(mysqli_stmt_bind_result, $fieldnames); mysqli_stmt_fetch($this->stmt); return $array; } ?> Hope this helps some people, I was puzzled by this for a while. --was-- I've created these functions which will act like mysqli_fetch_array() and mysqli_fetch_object() but work with bound results. <? function fetch_object() { $data = mysqli_stmt_result_metadata($this->stmt); $count = 1; //start the count from 1. First value has to be a reference to stmt. $fieldnames[0] = &$this->stmt; $obj = new stdClass; while ($field = mysqli_fetch_field($data)) { $fn = $field->name; //get all the feild names $fieldnames[$count] = &$obj->$fn; //load the fieldnames into an object.. $count++; } call_user_func_array(mysqli_stmt_bind_result, $fieldnames); mysqli_stmt_fetch($this->stmt); return $obj; } function fetch_array() { $data = mysqli_stmt_result_metadata($this->stmt); $count = 1; //start the count from 1. First value has to be a reference to the stmt. because bind_param requires the link to $stmt as the first param. $fieldnames[0] = &$this->stmt; while ($field = mysqli_fetch_field($data)) { $fieldnames[$count] = &$array[$field->name]; //load the fieldnames into an array. $count++; } call_user_func_array(mysqli_stmt_bind_result, $fieldnames); mysqli_stmt_fetch($this->stmt); return $array; } ?> Hope this helps some people, I was puzzled by this for a while. http://php.net/manual/en/function.mysqli-stmt-bind-result.php

« previous php.notes (#147363) next »