【发布时间】:2020-09-04 21:12:02
【问题描述】:
我有这段代码,我试图检查所选行中的所有值是否相同并且它只是不工作
$name = $row['name'];
$sql = "SELECT status FROM testing WHERE name='$name'";
$result = mysqli_query($conn, $sql);
$datas = array();
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$datas[] = $row;
}
}
if (count(array_unique($datas)) === 1 && end($datas) === 'No') {
echo 'all the same';
}
else {
echo 'not all the same';
}
在将值插入数组 $datas 之后,仍然尝试检查数据库中所有选定行中的值 No 是否相同,但它一直在抛出一个错误
注意:数组到字符串的转换
编辑:我添加了这个 $datas[] = $row['status']; 并且它起作用了
【问题讨论】:
-
警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDO 或MySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data。 Escaping is not enough!
标签: php mysql arrays count where-clause