mirror of
https://github.com/php/php-src.git
synced 2025-08-18 15:08:55 +02:00
Add PDOStatement::bindValue(), which is similar to bindParam(), except that
it binds the value of the zval at the time it is called, rather than keeping a reference to the zval and taking the value at execute() time.
This commit is contained in:
parent
9305339d94
commit
0730fa2af9
2 changed files with 75 additions and 0 deletions
|
@ -1393,6 +1393,36 @@ static int register_bound_param(INTERNAL_FUNCTION_PARAMETERS, pdo_stmt_t *stmt,
|
||||||
return really_register_bound_param(¶m, stmt, is_param TSRMLS_CC);
|
return really_register_bound_param(¶m, stmt, is_param TSRMLS_CC);
|
||||||
} /* }}} */
|
} /* }}} */
|
||||||
|
|
||||||
|
/* {{{ proto bool PDOStatement::bindValue(mixed $paramno, mixed $param [, int $type ])
|
||||||
|
bind an input parameter to the value of a PHP variable. $paramno is the 1-based position of the placeholder in the SQL statement (but can be the parameter name for drivers that support named placeholders). It should be called prior to execute(). */
|
||||||
|
static PHP_METHOD(PDOStatement, bindValue)
|
||||||
|
{
|
||||||
|
pdo_stmt_t *stmt = (pdo_stmt_t*)zend_object_store_get_object(getThis() TSRMLS_CC);
|
||||||
|
struct pdo_bound_param_data param = {0};
|
||||||
|
|
||||||
|
param.paramno = -1;
|
||||||
|
param.param_type = PDO_PARAM_STR;
|
||||||
|
|
||||||
|
if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC,
|
||||||
|
"lz/|l", ¶m.paramno, ¶m.parameter, ¶m.param_type)) {
|
||||||
|
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz/|l", ¶m.name,
|
||||||
|
¶m.namelen, ¶m.parameter, ¶m.param_type)) {
|
||||||
|
RETURN_FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (param.paramno > 0) {
|
||||||
|
--param.paramno; /* make it zero-based internally */
|
||||||
|
} else if (!param.name) {
|
||||||
|
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "Columns/Parameters are 1-based" TSRMLS_CC);
|
||||||
|
RETURN_FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
RETURN_BOOL(really_register_bound_param(¶m, stmt, TRUE TSRMLS_CC));
|
||||||
|
}
|
||||||
|
/* }}} */
|
||||||
|
|
||||||
|
|
||||||
/* {{{ proto bool PDOStatement::bindParam(mixed $paramno, mixed &$param [, int $type [, int $maxlen [, mixed $driverdata]]])
|
/* {{{ proto bool PDOStatement::bindParam(mixed $paramno, mixed &$param [, int $type [, int $maxlen [, mixed $driverdata]]])
|
||||||
bind a parameter to a PHP variable. $paramno is the 1-based position of the placeholder in the SQL statement (but can be the parameter name for drivers that support named placeholders). This isn't supported by all drivers. It should be called prior to execute(). */
|
bind a parameter to a PHP variable. $paramno is the 1-based position of the placeholder in the SQL statement (but can be the parameter name for drivers that support named placeholders). This isn't supported by all drivers. It should be called prior to execute(). */
|
||||||
static PHP_METHOD(PDOStatement, bindParam)
|
static PHP_METHOD(PDOStatement, bindParam)
|
||||||
|
@ -1835,6 +1865,7 @@ function_entry pdo_dbstmt_functions[] = {
|
||||||
PHP_ME(PDOStatement, fetch, NULL, ZEND_ACC_PUBLIC)
|
PHP_ME(PDOStatement, fetch, NULL, ZEND_ACC_PUBLIC)
|
||||||
PHP_ME(PDOStatement, bindParam, second_arg_force_ref, ZEND_ACC_PUBLIC)
|
PHP_ME(PDOStatement, bindParam, second_arg_force_ref, ZEND_ACC_PUBLIC)
|
||||||
PHP_ME(PDOStatement, bindColumn, second_arg_force_ref, ZEND_ACC_PUBLIC)
|
PHP_ME(PDOStatement, bindColumn, second_arg_force_ref, ZEND_ACC_PUBLIC)
|
||||||
|
PHP_ME(PDOStatement, bindValue, NULL, ZEND_ACC_PUBLIC)
|
||||||
PHP_ME(PDOStatement, rowCount, NULL, ZEND_ACC_PUBLIC)
|
PHP_ME(PDOStatement, rowCount, NULL, ZEND_ACC_PUBLIC)
|
||||||
PHP_ME(PDOStatement, fetchColumn, NULL, ZEND_ACC_PUBLIC)
|
PHP_ME(PDOStatement, fetchColumn, NULL, ZEND_ACC_PUBLIC)
|
||||||
PHP_ME(PDOStatement, fetchAll, NULL, ZEND_ACC_PUBLIC)
|
PHP_ME(PDOStatement, fetchAll, NULL, ZEND_ACC_PUBLIC)
|
||||||
|
|
44
ext/pdo/tests/pdo_028.phpt
Normal file
44
ext/pdo/tests/pdo_028.phpt
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
--TEST--
|
||||||
|
PDO Common: bindValue
|
||||||
|
--SKIPIF--
|
||||||
|
<?php # vim:ft=php
|
||||||
|
if (!extension_loaded('pdo')) die('skip');
|
||||||
|
$dir = getenv('REDIR_TEST_DIR');
|
||||||
|
if (false == $dir) die('skip no driver');
|
||||||
|
require_once $dir . 'pdo_test.inc';
|
||||||
|
PDOTest::skip();
|
||||||
|
?>
|
||||||
|
--FILE--
|
||||||
|
<?php
|
||||||
|
require getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||||
|
$db = PDOTest::factory();
|
||||||
|
|
||||||
|
$db->exec('CREATE TABLE test(id int NOT NULL PRIMARY KEY, val1 VARCHAR(10), val2 VARCHAR(10), val3 VARCHAR(10))');
|
||||||
|
$stmt = $db->prepare('INSERT INTO test values (1, ?, ?, ?)');
|
||||||
|
|
||||||
|
$data = array("one", "two", "three");
|
||||||
|
|
||||||
|
foreach ($data as $i => $v) {
|
||||||
|
$stmt->bindValue($i+1, $v);
|
||||||
|
}
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
|
$stmt = $db->prepare('SELECT * from test');
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
|
var_dump($stmt->fetchAll(PDO_FETCH_ASSOC));
|
||||||
|
?>
|
||||||
|
--EXPECT--
|
||||||
|
array(1) {
|
||||||
|
[0]=>
|
||||||
|
array(4) {
|
||||||
|
["id"]=>
|
||||||
|
string(1) "1"
|
||||||
|
["val1"]=>
|
||||||
|
string(3) "one"
|
||||||
|
["val2"]=>
|
||||||
|
string(3) "two"
|
||||||
|
["val3"]=>
|
||||||
|
string(5) "three"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue