Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP PDO Prepared Statements and Stored Procedures

PHP PDO Prepared Statements and Stored Procedures

PHP PDO Reference ManualPHP PDO Reference Manual

Many more mature databases support the concept of prepared statements.

What is a prepared statement? Think of it as a compiled template of the SQL you want to run, which can be customized with variable parameters. Prepared statements bring two major benefits:

Prepared statements are so useful that their only feature is that PDO will emulate them when the driver does not support them. This ensures that the application can use the same data access pattern regardless of whether the database has such a feature.

Repeated Insertion Using Prepared Statements

The example below performs an insert query by replacing the corresponding named placeholders with name and value



prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");

$stmt->bindParam(':name', $name);

$stmt->bindParam(':value', $value);



// Insert a line

$name = 'one';

$value = 1;

$stmt->execute();



// Insert another row with different values

$name = 'two';

$value = 2;

$stmt->execute();

?>

Repeated Insertion Using Prepared Statements

The example below performs an insert query by replacing the positions of the ? placeholders with name and value.



prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");

$stmt->bindParam(1, $name);

$stmt->bindParam(2, $value);



// Insert a line

$name = 'one';

$value = 1;

$stmt->execute();



// Insert another row with different values

$name = 'two';

$value = 2;

$stmt->execute();

?>

Fetching Data Using Prepared Statements

The example below fetches data based on the provided key-value form. User input is automatically quoted, so there is no risk of SQL injection attacks.



prepare("SELECT * FROM REGISTRY where name = ?");

if ($stmt->execute(array($_GET['name']))) {

  while ($row = $stmt->fetch()) {

    print_r($row);

  }

}

?>

If the database driver supports it, the application can also bind output and input parameters. Output parameters are typically used to get values from stored procedures. Output parameters are slightly more complex to use than input parameters because when binding an output parameter, you must know the length of the given parameter. If the value bound to the parameter is greater than the suggested length, an error is produced.

Calling a Stored Procedure with Output Parameters



prepare("CALL sp_returns_string(?)");

$stmt->bindParam(1, $return_value, PDO::PARAM_STR, 4000); 



// Call stored procedure

$stmt->execute();



print "procedure returned $return_value\n";

?>

You can also specify parameters that have both input and output values, with syntax similar to output parameters. In the next example, the string "hello" is passed to the stored procedure, and when the stored procedure returns, hello is replaced with the value returned by the stored procedure.

Calling a Stored Procedure with Input/Output Parameters



prepare("CALL sp_takes_string_returns_string(?)");

$value = 'hello';

$stmt->bindParam(1, $value, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 4000); 



// Call stored procedure

$stmt->execute();



print "procedure returned $value\n";

?>

Invalid Use of Placeholders



prepare("SELECT * FROM REGISTRY where name LIKE '%?%'");

$stmt->execute(array($_GET['name']));



// Placeholders must be used in the entire value position

$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name LIKE ?");

$stmt->execute(array("%$_GET[name]%"));

?>


PHP PDO Reference ManualPHP PDO Reference Manual