我试图使用foreach循环来显示我的数据库但我总是得到错误。我想要的是打印每一行和每列。我的代码是这样的:
$sql = "SELECT a.*, b.klasifikasi FROM kl_stre as b
INNER JOIN data_latih as a
ON a.id_stres = b.id_stres
ORDER BY a.id_dl";
$result = mysqli_query($conn, $sql);
foreach ($result as $dt_train => $row_dt_train):
foreach ($row_dt_train as $attr => $attr_dt_train):
echo $result[$row_dt_train][$attr_dt_trian]; // this line is the problem
endforeach;
endforeach;
我得到的错误是
警告:第43行的C:\ xampp \ htdocs \ knn \ array_db.php中的非法偏移类型
您是否介意解释此代码的错误以及如何解决此问题?
答案 0 :(得分:0)
您需要先查看您的查询
foreach
在获取记录之后,只需删除while()
并尝试使用while ($row=mysqli_fetch_array($result)) {
echo $row['your database field name'];
}
循环
$sql = "SELECT a.*, b.klasifikasi FROM kl_stre as b
INNER JOIN data_latih as a
ON a.id_stres = b.id_stres
ORDER BY a.id_dl";
$result = mysqli_query($conn, $sql);
if (!$result) {
die('query is not correct'.mysqli_error($conn));
}
while ($row=mysqli_fetch_array($result)) {
echo $row['your database field name'];
}
您的代码将
AnyCPU