User Tools

Site Tools


libraries:databaseconnector:databaseconnectorclass:sqlbindpreparedparam

DatabaseConnector::SqlBindPreparedParam()


Definition

Binds a variable via reference to a placeholder of a prepared statement. The variable is bound to a placeholder within the prepared statement (placeholder can be a name in the format “:name” or “?” for a index based binding. Every time the prepared statement is executed, the current value from the bound variable is used.

Bool SqlBindPreparedParam ( $Parameter, &$Variable, $DataType = PDO::PARAM_STR )

Parameters

  • $Parameter Mixed
    Name (String) of the placeholder that was used in the prepared statement (like “:name”) or 1-based index (Integer) of the placeholders “?”.
  • &$Variable Mixed
    Variable which should be bound with the used placeholder. The variable is bound by reference.
  • $DataType Integer (optional)
    Optional datatype of the parameter as integer from the collection PDO::PARAM_xxx which controls, how the variable is handled within the query.
    Default is PDO::PARAM_STR.

Returns

  • Bool
    TRUE if the variable could be bould properly or FALSE if an error occured (see also GetLastError()).

Example

require_once 'DatabaseConnector/DatabaseConnector.php';
 
$DB = new DatabaseConnector();
 
// Connection to a SQLite3 database
$DB->ConnectSQLite3( '/htdocs/data/database1.sqlite3' );
 
// Preparing the statement with two index-based placeholders and one by name
$DB->SqlPrepareStatement("SELECT id, username FROM #_users WHERE city =':cityname' AND birthyear BETWEEN ? AND ?");
 
// Creating Variables to prepare the binding
$strNameOfCity = 'Chicago';
$iYearFrom     = 1970;
$iYearTo       = 2011;
 
// Binding the paramters by reference
$DB->SqlBindPreparedParam(':cityname', $strNameOfCity);
$DB->SqlBindPreparedParam(1, $iYearFrom, PDO::PARAM_INT);
$DB->SqlBindPreparedParam(2, $iYearTo, PDO::PARAM_INT);
 
// Getting the result of users in 'Chicago' which were born between 1970 and 2011
$arrResult = $DB->SqlGetPreparedLines();
 
// [...]
 
// Changing bound parameters
$strNameOfCity = 'Boston';
$iYearTo       = 2020;
 
// Getting the result of users in 'Boston' which were born between 1970 and 2020
$arrResult = $DB->SqlGetPreparedLines();
 
// [...]
libraries/databaseconnector/databaseconnectorclass/sqlbindpreparedparam.txt · Last modified: 2023/03/03 14:09 by michael.pohl