我有一个脚本可以从两个表matieres
和sous_matieres
中提取数据,但它是为 PDO 编写的,我需要它来为工作mysqli的:
$stmt = $pdo->query('SELECT
m.id AS m_id, m.url AS m_url, m.title AS m_title,
s.id AS s_id, s.url AS s_url, s.title AS s_title
FROM matieres m
INNER JOIN sous_matieres s ON m.url = s.parent');
while ($row = $stmt->fetchObject()) {
$matieres[$row->m_id]['url'] = $row->m_url;
$matieres[$row->m_id]['title'] = $row->m_title;
$matieres[$row->m_id]['sous_matieres'][$row->s_id] = $row;
}
foreach ($matieres as $m_id => $matiere) {
echo "<h2>$matiere[title]</h2>";
foreach ($matiere['sous_matieres'] as $id => $sm) {
echo "<div>
<a href='{$sm->s_url}'>{$sm->s_title}</a>
</div>";
}
}
我的新 mySQLi 代码尽管两个表都有数据但没有显示任何内容:
$query = 'SELECT
m.id AS m_id, m.url AS m_url, m.title AS m_title,
s.id AS s_id, s.url AS s_url, s.title AS s_title
FROM matieres m
INNER JOIN sous_matieres s ON m.url = s.parent';
$stmt = $mysqli->query($query);
$stmt->execute();
$stmt->store_result();
while ($row = $stmt->fetch()) {
$matieres[$row->m_id]['url'] = $row->m_url;
$matieres[$row->m_id]['title'] = $row->m_title;
$matieres[$row->m_id]['sous_matieres'][$row->s_id] = $row;
}
foreach ($matieres as $m_id => $matiere) {
echo "<h2>$matiere[title]</h2>";
foreach ($matiere['sous_matieres'] as $id => $sm) {
echo "<div>
<a href='{$sm->s_url}'>{$sm->s_title}</a>
</div>";
}
}
我不知道它是否遗漏了$stmt->bind_result()
以及里面的变量。对于正常的mySQLi查询(没有连接),我必须在$stmt->bind_result()
答案 0 :(得分:1)
mysqli_stmt::fetch
来填充这些变量。获得结果的另一种方法是使用mysqli_result
类,您可以按照以下方式使用:
$result = $mysqli->query($query);
while ($row = $result->fetch_object()) {
$matieres[$row->m_id]['url'] = $row->m_url;
$matieres[$row->m_id]['title'] = $row->m_title;
$matieres[$row->m_id]['sous_matieres'][$row->s_id] = $row;
}
请参阅http://php.net/manual/en/mysqli.query.php和http://php.net/manual/en/class.mysqli-result.php。