020 555 1111
  • Old fashion, modular, procedural PHP plus some scaffolding techniques to allow it to mimic MVC simplicity.
  • A quick reference to help you find in PHP maze what needs to be done and how to do it.
  • Learn a PHP Object Oriented Programming by building a live discussion forum application.
  • MVC - Model View Controller - real life, easy reference and application.
This product has options, take a look at them and select ones you like the most.
Options are displayed under image section.

PDO: Binding params and results
[MySQL PDO Statements]

 
Binding params to sql template 
 
PDOStatement::bindParam
Binds a parameter to the specified variable name.
It will be used by prepared SQL template to fetch results.
Binds a PHP variable to a corresponding named or question mark placeholder in the SQL statement that was use to prepare the statement. 
Unlike PDOStatement::bindValue(), the variable is bound as a reference (passed by reference) and will only be evaluated at the time that PDOStatement::execute() is called.
In other words: you do not know outright value bound as param.
Rule of thumb you should use bindParam() when you have to pass multiple data (e.g. all values of an array).
 
In simple wordsPDOStatement::bindParam() is used on the right hand side of WHERE condition.

More: here.
 
 
PDOStatement::bindValue
Binds a value to a parameter.
It will be used by prepared SQL template to fetch results.
Binds a value to a corresponding named or question mark placeholder in the SQL statement that was used to prepare the statement.
Unlike PDOStatement::bindParam(), the variable is not bound as a reference, it is passed by value.
 
Rule of thumb you should use bindValue() when you have to pass data only once.
 
In simple wordsPDOStatement::bindValue() is used on the right hand side of WHERE condition.

More: here.
 
 
For difference between passing by reference and by value, try this link.

 

$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD);
$sql = 'SELECT entry_title, entry_body FROM posts WHERE entry_date < :entry_date';
$sth = $dbh->prepare($sql);

$entry_date = '1328216648';
$sth->bindParam(':entry_date', $entry_date, PDO::PARAM_INT);
//or
$sth->bindValue(':entry_date', $entry_date, PDO::PARAM_INT);
$sth->execute();

This is similar to MySQLi code:

$stmt->bind_param("s",$entry_date);
 
 
Assign returned result values to variable for further use 
 
 
PDOStatement::bindColumn
Bind a column to a PHP variable.
PDOStatement::bindColumn() arranges to have a particular variable bound to a given column in the result-set from a query. 
Each call to PDOStatement::fetch() or PDOStatement::fetchAll() will update all the variables that are bound to columns.
 
In simple wordsPDOStatement::bindColumn() is used on the left hand side of WHERE condition and deals with results.

More: here.
 
Since information about the columns is not always available to PDO until the statement is executed, portable applications should call this function after PDOStatement::execute().
//FROM_UNIXTIME(:entry_date, '%Y-%m-%d %H:%i:%s') formats date properly - according to MySQL storage standard for datetime, e.g.: 2012-02-13 04:28:54
//one could use php to format data: $entry_date = time("Y-m-d H:i:s", 1329109080);
$sql = "SELECT entry_title, entry_body FROM posts WHERE entry_date < FROM_UNIXTIME(:entry_date, '%Y-%m-%d %H:%i:%s') ";
$sth = $dbh->prepare($sql);

$entry_date = '1329109080';
$sth->bindParam(':entry_date', $entry_date, PDO::PARAM_STR);
$sth->execute();

//it is a good way to use bindVolumn below execute() - at least for MySQL
$sth->bindColumn('entry_title', $entry_title, PDO::PARAM_STR);

//DO NOT USE BOTH: fetch() and fetchAll(), if you try this code

//fetch()
//this will display first result and stops
$result = $sth->fetch();

//this will show full result set listed
while ($result = $sth->fetch()) {
    echo $entry_title;
}

//fetchAll()
//this keeps overwriting (without loop) until it shows last fro result range
$result = $sth->fetchAll();

//this will show full result set listed
foreach ($result as $k => $row) {
    echo $row['entry_title'];
}
This is similar to MySQLi code:
$stmt->bind_result($entry_id, $user_id //mysqli