xref: /PHP-8.2/ext/pdo/tests/pdo_021.phpt (revision 74859783)
1--TEST--
2PDO Common: PDOStatement::execute with parameters
3--EXTENSIONS--
4pdo
5--SKIPIF--
6<?php
7$dir = getenv('REDIR_TEST_DIR');
8if (false == $dir) die('skip no driver');
9require_once $dir . 'pdo_test.inc';
10PDOTest::skip();
11?>
12--FILE--
13<?php
14if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/');
15require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
16$db = PDOTest::factory();
17
18if ($db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') {
19    $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
20}
21
22$db->exec('CREATE TABLE test(id INT NOT NULL PRIMARY KEY, val VARCHAR(10), val2 VARCHAR(16))');
23
24$select = $db->prepare('SELECT COUNT(id) FROM test');
25
26$data = array(
27    array('10', 'Abc', 'zxy'),
28    array('20', 'Def', 'wvu'),
29    array('30', 'Ghi', 'tsr'),
30    array('40', 'Jkl', 'qpo'),
31    array('50', 'Mno', 'nml'),
32    array('60', 'Pqr', 'kji'),
33);
34
35
36// Insert using question mark placeholders
37$stmt = $db->prepare("INSERT INTO test VALUES(?, ?, ?)");
38foreach ($data as $row) {
39    $stmt->execute($row);
40}
41$select->execute();
42$num = $select->fetchColumn();
43echo 'There are ' . $num . " rows in the table.\n";
44
45// Insert using named parameters
46$stmt2 = $db->prepare("INSERT INTO test VALUES(:first, :second, :third)");
47foreach ($data as $row) {
48    $stmt2->execute(array(':first'=>($row[0] + 5), ':second'=>$row[1],
49        ':third'=>$row[2]));
50}
51
52$select->execute();
53$num = $select->fetchColumn();
54echo 'There are ' . $num . " rows in the table.\n";
55
56
57?>
58--EXPECT--
59There are 6 rows in the table.
60There are 12 rows in the table.
61