pdo_021.phpt 1.5 KB

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