note 92948 deleted from mysqli-stmt.bind-result by danbrown
| From: | danbrown@php.net | Date: | Sun, 16 Aug 2009 18:08:19 +0000 |
| Subject: | note 92948 deleted from mysqli-stmt.bind-result by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-159549@lists.php.net to get a copy of this message | ||
Note Submitter: jhr3, Spagyria, com
----
<?
class Database
{
private $ReturnItem;
private $Stmt;
private $GVS;
private $mysqli;
private $Metadata;
private $CountLimit;
private $Pointers = array();
private $BindDynamicVariables;
function __construct()
{
$this -> mysqli = new XXX();
$this -> BindDynamicVariables = new stdClass();
}
public function selectAllExceute($SelectQuery)
{
$this -> Stmt = $this -> mysqli -> prepare($SelectQuery);
$this -> Stmt -> execute();
$this -> Stmt -> store_result();
if ($this ->Stmt->num_rows > 0)
{
$this -> Metadata = $this -> Stmt -> result_metadata();
$this -> Fields = $this -> Metadata -> fetch_fields();
foreach ($this -> Fields as $Field)
{
/* assign coumn names from table corresponding to your sql query*/
$Fieldname = $Field->name;
/* The array named pointers is just a bounce board that is used to cause StdClass instantiated
through the variable $BindDynamicVariables to dynamically create variables named after the column
names returned from the query.
Anyway at this point we have created variables in StdClass that corresponds to the column names of
your SQL query, but no value is assigned to them yet. I was confused as to how this was being done
until I read the section in the manual "What References Do" and the note which stated: If
you assign, pass or return an undefined variable by reference, it will get created.*/
$this -> Pointers[] = &$this -> BindDynamicVariables->$Fieldname;
}
/*Now tell the engine where the data which will be retrieved from the rows is to be bound. Use the
call_user_func_array give it the statement and the actual function to be called bind_result in this
case and where it can find the variables to bind the results to. One can use either the $this ->
Pointers array , or the BindDynamicVariables here*/
call_user_func_array(array($this ->Stmt, 'bind_result'), $this ->
BindDynamicVariables);
return $this ->Stmt->num_rows;
}
}
public function GetDataFromRow($DataSeekRowNumber)
{
$this -> CountLimit = $this ->Stmt->num_rows -1;
if ($DataSeekRowNumber > $this -> CountLimit) return "Append Instructions";
$this -> Stmt -> data_seek($DataSeekRowNumber);
if (!$this -> Stmt->fetch()) return "Append Instructions";
$this -> ReturnItem = array();
$i = 0;
foreach ($this -> Fields as $Field)
{
$Fieldname = $Field->name;
/*Place data from result set into their respective array slots. Access the dynamic variables
created in StdClass through the Instantiated variable as you would any other class variable pair. if
you wanter to use the array $Pointers instead of the dyanic varibles you would replace "$this
-> BindDynamicVariables -> $Fieldname" with "$this -> Pointers[$i]"*/
$this -> ReturnItem[$i] = $this -> BindDynamicVariables -> $Fieldname;
$i++;
}
return $this -> ReturnItem;
}
}
?>
<?
/* from another page which has the above class loaded */
/* instantiate class */
$Database = new Database();
/* create query string */
$MyQuery = "select col1,col2,col3 from tablename";
/* call to this function returns row count but also causes query to be excuted and entire result set
to be stored */
$RowCount = $Database -> selectALLExceute($MyQuery);
/* create a loop to call GetDataFromRow function for as many rows as needed */
for ($i = 0; $i <= $RowCount; $i++)
{
/* create a loop to call GetDataFromRow function for as many rows as needed. The variable $Result is
an array whom's elements may be accessed via offset number*/
$Result = $Database -> GetDataFromRow($i);
/* print each array representing a row in the result set. */
print_r($Result);
echo("<br>");
}
?>