讀取資料。
簡述
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <?php require_once('conn.php');
$result = $conn->query('SELECT * FROM users ORDER BY id ASC');
if (!$result) { die($conn->error); } $format = ' <div class="user"> <p class="user__id">id: %d</p> <p class="user__name">username: %s</p> <a class="user__button" href="delete.php?id=%d">刪除</a> </div> ';
while($row = $result->fetch_assoc()) { echo sprintf($format, $row['id'], $row['username'], $row['id'] ); } ?>
|
關於 $result->fetch_assoc()
的部分可以想成是這樣:
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?php $row = $result->fetch_assoc(); print_r($row);
$row = $result->fetch_assoc(); print_r($row);
$row = $result->fetch_assoc(); print_r($row); ?>
|