Jump to content

Recommended Posts

Hello everybody

this is a script from a tutorial on tutsplus.com

 

I do not know what this variable $row (red-colored) means really. Where does it belong to? thanks

<?php

/*

* This class has all the functions to work with mysql-PHP, and all of them are designed to be

* flexible with every project.

*/

class SQL

{

protected $_statement;

protected $_qStatement;

 

function __construct($host, $user, $pass, $database)

{

$this->_statement = new mysqli($host, $user, $pass, $database);

}

/**

*

* @param string $query It receives a set of strings as SQL statement.

*/

function Query($query)

{

$this->_qStatement = filter_var($query, FILTER_SANITIZE_STRING);

$stmt = $this->Prepare();

$stmt->execute();

$result = $this->dynamicBindResult($stmt);

$stmt->fetch();

}

protected function Prepare()

{

if(!$stmt=$this->_statement->prepare($this->_qStatement))

{

trigger_error("No preparation has been done. Please correct the statement or syntax-contaminated region.", E_USER_ERROR);

}

return $stmt;

}

protected function dynamicBindResult($stmt)

{

$paramatere = array();

$results = array();

$name = "";

$result = $stmt->result_metadata();

while($field = $result->fetch_field())

{

$paramatere[] = &$row[$field->name];

}

call_user_func_array(array($stmt, "bind_result"), $paramatere);

while($stmt->fetch())

{

$x = array();

foreach($paramatere as $keyName => $keyValue)

{

$x[$keyValue] = $keyName;

}

$results = $x;

}

return $results;

}

 

}

Link to comment
https://forums.phpfreaks.com/topic/271580-what-this-variable-means/
Share on other sites

The code itself is simple but what's going on behind the scenes is definitely worth an explanation.

 

$row doesn't exist, as I'm sure you've noticed. But in this context, PHP will create both $row and $row[$field->name] (with a null value) automatically. The proper way of writing that loop would be

$row = array();
while($field = $result->fetch_field())
    {
$row[$field->name] = null;
$paramatere[] = &$row[$field->name];
    }

 

The next thing to see is the &. That means "reference to", as in "$paramatere[] is a reference to $row[$field->name]". Normal assignment means just "a copy of", but with this the two variables actually refer to the same thing, and if you changed the value of one then the other would be "updated" automatically.

So now $paramatere is an array with a lot of references to stuff in $row - one for each field. The array then goes into $stmt->bind_param() by way of call_user_func_array().

 

As for why it does this? mysqli_stmt::bind_result() and ::bind_param() are not normal functions in the sense that you pass in values as arguments and they return values to you. Instead you pass in variables, as references, and the functions then modify (bind_result) or repeatedly access (bind_param) the values with no extra effort on your part. To do this they both require variables which are references - you can't just pass in a variable normally because that'll just be a copy and the functions won't work.

The whole point of that $row stuff and references and call_user_func_array() are to pass in an unknown number of variable references to the function.

Edited by requinix
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.