Eu descobri um erro quando estava fazendo o meu script em PHP usando o conector PDO.
(I’ve found a bug when working on a PHP script using a PDO connector.)
Quando fiz uma Stored Procedure usando o recurso PREPARE dentro, ocorreu um problema com o resultado do meu script em PHP.
(When I made a Store Procedure using the PREPARE statement something messed up the result im my PHP script.)
Config:
System: Linux Debian 2.6.21-2-486
Apache: Apache 2.0 Handler
PHP: Version 5.2.3-1+b1
MySQL: 5.0.45
Seguem abaixo os códigos:
(The code is shown below:)
Estrutura da tabela (Table Structure)
CREATE TABLE `tb_user` (
`cd_user` int(11) NOT NULL auto_increment,
`login` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
PRIMARY KEY (`cd_user`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- INSERT tb_user
--
INSERT INTO `tb_user` VALUES (1, 'lula', 'lele');
INSERT INTO `tb_user` VALUES (2, 'php', 'cool');
Stored Procedure SELECT
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`sp_test_select`$$
CREATE PROCEDURE `test`.`sp_test_select` ()
BEGIN
SELECT login,password FROM test.tb_user;
END$$
DELIMITER ;
Stored Procedure PREPARE
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`sp_test_prepare`$$
CREATE PROCEDURE `test`.`sp_test_prepare` ()
BEGIN
PREPARE s1 FROM 'SELECT login,password FROM test.tb_user';
EXECUTE s1;
DEALLOCATE PREPARE s1;
END$$
DELIMITER ;
Código PHP (PHP Code)
function result($sql)
{
$db = new PDO('mysql:dbname=test;host=127.0.0.1', 'test','test');
$stm = $db->prepare($sql);
$stm->execute();
echo "\n######## $sql #######\n";
while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
echo $row["login"]." - ".$row["password"]."\n";
}
$db = null;
$stm = null;
}
result("SELECT * FROM tb_user");
result("CALL sp_test_select()");
result("CALL sp_test_prepare()");
Resultado (Result)
zed:/www/directdial/tmp# php pdo.php
######## SELECT * FROM tb_user #######
lula - lele
php - cool
######## CALL sp_test_select() #######
lula - lele
php - cool
######## CALL sp_test_prepare() #######
-
pcoolb_usertb_usepassworpassword
ý¨ˆ”�½test -
Aqui esta o problema, quando chamo CALL sp_test_prepare() pelo PDO , ele não retorna corretamente os valores.
(Here lies the problem. When I call CALL sp_test_prepare() through PDO, it does not return the values as it should.)
Se você sabe como resolver este problema, por favor poste um comentário.
(If you know how to fix this bug, please comment on this thread.)
Resolução
Encontrei a resposta no seguinte link:
http://pecl.php.net/bugs/bug.php?id=7365
