User Tools

Site Tools


libraries:databaseconnector:databaseconnectorclass:sqlbindpreparedvalue

DatabaseConnector::SqlBindPreparedValue()


Definition

Binds a value or a variable (by value) 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 same value from the previously bound variable is used.

Bool SqlBindPreparedValue ( $Parameter, $Value, $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 “?”.
  • $Value Mixed
    Value or Variable (by value) which should be bound with the used placeholder. The variable is bound by value.
  • $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 value/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
$iYearFrom     = 1970;
$iYearTo       = 2011;
 
// Binding the paramters by value
$DB->SqlBindPreparedValue(':cityname', 'Chicago');
$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 the parameters
$strNameOfCity = 'Boston';
$iYearTo       = 2020;
 
// Getting the result of users in 'Chicago' which were born between 1970 and 2011
// No changes in values!
$arrResult = $DB->SqlGetPreparedLines();
 
// [...]
libraries/databaseconnector/databaseconnectorclass/sqlbindpreparedvalue.txt · Last modified: 2023/03/03 14:33 by michael.pohl