|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-12-06 06:17 UTC] capiCrimm at gmail dot com
Description:
------------
Using named and positional :placeholders in a PDO prepare
statement result in an empty table in MySQL. Entering in
the values into the prepare statement by hand does work,
however, and when copying over the examples from the man
to check myself I could get a nonsensical
result(196864:327683) when using 5 for the values. I added
a print in the code, but the values are also blank using
mysql CLI and the result is the same using php CLI
Reproduce code:
---------------
<?php
$conn = new PDO('mysql:dbname=testing;host=localhost;','root','');
## CREATE TABLE tbTest(one char(4), two char(4));
$stmt = $conn->prepare("INSERT INTO tbTest(one,two) VALUES (:name , :value)");
$stmt->bindParam(':name', $name, PDO::PARAM_STR, 4);
$stmt->bindParam(':value', $value, PDO::PARAM_STR, 4);
$name = 'val1';
$value = 'val2';
$stmt->execute();
foreach( $conn->query("SELECT * FROM tbTest", PDO::FETCH_ASSOC) as $table ){
print " {$table['one']}:{$table['two']} ";
}
?>
Expected result:
----------------
val1:val2
Actual result:
--------------
:
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 28 11:00:02 2025 UTC |
I have encountered this problem using PHP 5.1.2 with MySQL 4.1.12. However, MySQL 5.0.18 returns the expected results. I was unable to install the latest CVS copy of PHP 5.1, but might try again later when I have more time. <?php $id = '1'; $conn = new PDO('mysql:dbname=test;host=localhost;','root','worksql'); # CREATE TABLE `testtable` ( `test_id` INT NOT NULL ,`test_data` VARCHAR( 128 ) NOT NULL ); # INSERT INTO `testtable` ( `test_id` , `test_data` ) VALUES ( '1', 'test data 1' ), ( '2', 'test data 2' ); $query = $conn->prepare("SELECT * FROM testtable WHERE test_id=?"); $query->execute(array($id)); $results = $query->fetchAll(PDO::FETCH_ASSOC); var_dump($results); ?> MySQL 4.1.12 returns: array(0) { } MySQL 5.0.18 returns: array(1) { [0]=> array(2) { ["test_id"]=> string(1) "1" ["test_data"]=> string(11) "test data 1" } }