Войти через VK Войти через FB Войти через Google Войти через Яндекс
Поиск по сайту
$sql = 'SELECT name, breed, weight FROM animals
WHERE weight > ? AND weight < ?';
$conn = db2_connect($database, $user, $password);
$stmt = db2_prepare($conn, $sql);
// We can declare the variable before calling db2_bind_param()
$lower_limit = 1;
db2_bind_param($stmt, 1, "lower_limit", DB2_PARAM_IN);
db2_bind_param($stmt, 2, "upper_limit", DB2_PARAM_IN);
// We can also declare the variable after calling db2_bind_param()
$upper_limit = 15.0;
if (db2_execute($stmt)) {
while ($row = db2_fetch_array($stmt)) {
print "{$row[0]}, {$row[1]}, {$row[2]}\n";
}
}
Результат выполнения данного примера:
Pook, cat, 3.2
Rickety Ride, goat, 9.7
Peaches, dog, 12.3
Пример #2 Calling stored procedures with IN and OUT parameters
The stored procedure match_animal in the following example accepts three different parameters:
- an input (IN) parameter that accepts the name of the first animal as input
- an input-output (INOUT) parameter that accepts the name of the second animal as input and returns the string TRUE if an animal in the database matches that name
- an output (OUT) parameter that returns the sum of the weight of the two identified animals
$sql = 'CALL match_animal(?, ?, ?)';
$conn = db2_connect($database, $user, $password);
$stmt = db2_prepare($conn, $sql);
$name = "Peaches";
$second_name = "Rickety Ride";
$weight = 0;
db2_bind_param($stmt, 1, "name", DB2_PARAM_IN);
db2_bind_param($stmt, 2, "second_name", DB2_PARAM_INOUT);
db2_bind_param($stmt, 3, "weight", DB2_PARAM_OUT);
print "Values of bound parameters _before_ CALL:\n";
print " 1: {$name} 2: {$second_name} 3: {$weight}\n\n";
if (db2_execute($stmt)) {
print "Values of bound parameters _after_ CALL:\n";
print " 1: {$name} 2: {$second_name} 3: {$weight}\n\n";
print "Results:\n";
while ($row = db2_fetch_array($stmt)) {
print " {$row[0]}, {$row[1]}, {$row[2]}\n";
}
}
Результат выполнения данного примера:
Values of bound parameters _before_ CALL:
1: Peaches 2: Rickety Ride 3: 0
Values of bound parameters _after_ CALL:
1: Peaches 2: TRUE 3: 22
Results:
Peaches, dog, 12.3
Pook, cat, 3.2
Rickety Ride, goat, 9.7
Пример #3 Inserting a binary large object (BLOB) directly from a file
The data for large objects are typically stored in files, such as XML documents or audio files. Rather than reading an entire file into a PHP variable, and then binding that PHP variable into an SQL statement, you can avoid some memory overhead by binding the file directly to the input parameter of your SQL statement. The following example demonstrates how to bind a file directly into a BLOB column.
$stmt = db2_prepare($conn, "INSERT INTO animal_pictures(picture) VALUES (?)");
$picture = "/opt/albums/spook/grooming.jpg";
$rc = db2_bind_param($stmt, 1, "picture", DB2_PARAM_FILE);
$rc = db2_execute($stmt);
Смотрите также
- db2_execute() - Executes a prepared SQL statement
- db2_prepare() - Prepares an SQL statement to be executed

User Contributed Notes
There are no user contributed notes for this page.