From: Wez Furlong Date: Sat, 10 Sep 2005 15:32:04 +0000 (+0000) Subject: Add PDOStatement::bindValue(), which is similar to bindParam(), except that X-Git-Tag: RELEASE_1_0RC1~15 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0730fa2af9aba352829f3f27f56721a361b10ab0;p=php 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. --- diff --git a/ext/pdo/pdo_stmt.c b/ext/pdo/pdo_stmt.c index 1dd5c79a0f..6201790dfd 100755 --- a/ext/pdo/pdo_stmt.c +++ b/ext/pdo/pdo_stmt.c @@ -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); } /* }}} */ +/* {{{ 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]]]) 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) @@ -1835,6 +1865,7 @@ function_entry pdo_dbstmt_functions[] = { PHP_ME(PDOStatement, fetch, NULL, 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, bindValue, NULL, ZEND_ACC_PUBLIC) PHP_ME(PDOStatement, rowCount, NULL, ZEND_ACC_PUBLIC) PHP_ME(PDOStatement, fetchColumn, NULL, ZEND_ACC_PUBLIC) PHP_ME(PDOStatement, fetchAll, NULL, ZEND_ACC_PUBLIC) diff --git a/ext/pdo/tests/pdo_028.phpt b/ext/pdo/tests/pdo_028.phpt new file mode 100644 index 0000000000..341917605f --- /dev/null +++ b/ext/pdo/tests/pdo_028.phpt @@ -0,0 +1,44 @@ +--TEST-- +PDO Common: bindValue +--SKIPIF-- + +--FILE-- +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" + } +}