我试图在同一个循环中使用2个预处理语句,我试图在一个查询中占用所有字段,但我不能因为我的bbdd模型(有时第一个查询给出行,有时不行,第二个查询相同)。这是代码,我试图尽可能地简化它:
<?php
if ($db -> rows($sql) > 0) {
// Prepared SQL1:
$psql1 = "SELECT user FROM users WHERE ID=?;";
$prepare_sql1 = $db -> prepare($psql1);
$prepare_sql1 -> bind_param('i', $topic);
// Prepared SQL2:
$psql2 = "SELECT US.ID,
US.user,
PT.date,
PT.time
FROM posts PT,
users US
WHERE ID_topic=?
AND US.ID = PT.owner
ORDER BY PT.ID DESC LIMIT 1;";
$prepare_sql2 = $db -> prepare($psql2);
$prepare_sql2 -> bind_param('i', $id_topic);
while ($x = $db -> fetch($sql)) {
// Prepared SQL1:
$id_autor_topic = $x['owner'];
$prepare_sql1 -> execute();
$prepare_sql1 -> bind_result($autor_topic);
$prepare_sql1 -> fetch();
// Prepared SQL2:
$id_topic = $x['id'];
$prepare_sql2 -> execute();
$prepare_sql2 -> bind_result($id, $user, $date, $time);
$prepare_sql2 -> fetch();
}
$prepare_sql1 -> close();
$prepare_sql2 -> close();
}
?>
如果我只使用一个,但只有一个,但两个只有第一个工作,两个预备语句都有效。我搜索了一个解决方案并尝试了很多东西,但是有什么工作,有什么建议吗?