我正在努力应对fetchAll()的怪异行为。我正在使用PDO执行存储过程,返回歌曲集。
这是我的PHP代码
$sql = "call getHymn(?)";
$param = array($id);
$result = $this->db->getData($sql,$param);
这是getData
函数
public function getData($sql,$param=[])
{
try
{
$stmt = $this->connection->prepare($sql);
$stmt->execute($param);
echo '<pre>';
print_r($stmt->fetchAll());
/*return($stmt->fetchAll());*/
}
catch (PDOException $e)
{
throw new Exception($e->getMessage());
}
}
当我在MySQL中执行存储过程时,它会按预期返回3行。但是当我通过PDO执行它并使用fetchall()然后我在数组外部得到额外的null
..在任何地方都找不到任何帮助,甚至在PHP文档中都没有。我的fetchAll()输出如下所示
Array
(
[0] => Array
(
[id] => 4
[refrain] =>
[stanzaId] => 1
[stanzaText] => Amazing grace! How sweet the sound
that saved a wretch like me!
I once was lost, but now am found;
was blind, but now I see.
)
[1] => Array
(
[id] => 4
[refrain] =>
[stanzaId] => 2
[stanzaText] => 'Twas grace that taught my heart to fear,
and grace my fears relieved;
how precious did that grace appear
the hour I first believed.
)
[2] => Array
(
[id] => 4
[refrain] =>
[stanzaId] => 3
[stanzaText] => Through many dangers, toils, and snares,
I have already come;
'tis grace hath brought me safe thus far,
and grace will lead me home
)
)
null
这是我的存储过程非常简单直接
CREATE DEFINER=`root`@`localhost` PROCEDURE `getHymn`(IN `hid` INT)
READS SQL DATA
BEGIN
select
a.id, a.refrain, b.stanzaId, b.stanzaText
from
hymns a, hymnstanza b
where a.id = hid and a.id = b.hymnid;
END
有人能帮我理解发生了什么吗?
答案 0 :(得分:0)
好的,朋友们。我现在发现了这个bug,并且@decez你是绝对正确的,因为有一些额外的echo语句导致在打印数组后出现null
。在将近12个小时之后感觉相当轻松,我能够顺利前进。特别感谢@decez。
为了详细说明我做了什么错误,我对fetchAll()的输出进行了两次JSON化。