循环直到它变为空数据库字段

时间:2017-10-24 08:19:50

标签: php

此代码有效:

$i=1;
require('db.php');
$query = "SELECT own".$i." FROM users WHERE username='".$_SESSION['username']."'";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($result);

所以我试着将它放入循环中,但它不起作用。我只获得了第一个结果。 甚至可以这样做吗?

while (isset($row['own'.$i])) {
require('db.php');
$query = "SELECT own".$i." FROM users WHERE username='".$_SESSION['username']."'";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($result);
 echo $row['own'.$i];
 $i++;
}

我也使用上面的代码在循环之前进行第一次检查

1 个答案:

答案 0 :(得分:0)

您始终只选择SELECT own1

您需要foreach而不是while

require('db.php');

$query = "SELECT * FROM users WHERE username='".$_SESSION['username']."'"; // SQL Injection HERE!
$result = mysqli_query($con, $query);
$rows = mysqli_fetch_assoc($result);

foreach ($rows as $row)) {
   var_dump($row);
}