]> granicus.if.org Git - php/commitdiff
Add PDOStatement::bindValue(), which is similar to bindParam(), except that
authorWez Furlong <wez@php.net>
Sat, 10 Sep 2005 15:32:04 +0000 (15:32 +0000)
committerWez Furlong <wez@php.net>
Sat, 10 Sep 2005 15:32:04 +0000 (15:32 +0000)
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.

ext/pdo/pdo_stmt.c
ext/pdo/tests/pdo_028.phpt [new file with mode: 0644]

index 1dd5c79a0f65500e1f39e0d39b5b0f4dae20b925..6201790dfd71c4421fc77144834e5460486186f5 100755 (executable)
@@ -1393,6 +1393,36 @@ static int register_bound_param(INTERNAL_FUNCTION_PARAMETERS, pdo_stmt_t *stmt,
        return really_register_bound_param(&param, 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", &param.paramno, &param.parameter, &param.param_type)) {
+               if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz/|l", &param.name,
+                               &param.namelen, &param.parameter, &param.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(&param, 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 (file)
index 0000000..3419176
--- /dev/null
@@ -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"
+  }
+}