如何使用php while循环从sql制作一个列表

时间:2022-10-16 12:10:14

So I have a database table, named todolist, and I want to display the whole to do list in a box. What I have right now is this:

所以我有一个名为todolist的数据库表,我想在一个框中显示整个待办事项列表。我现在拥有的是:

$db = (INFO HERE :));    
$sql = "SELECT * FROM todolist";
$result = mysqli_query($db, $sql);
if (mysqli_num_rows($result) == 0) {
  echo "There is nothing else to do! :)";
} else {
}

What do I put in the else, for it to display everything on a list? Thanks

我在其他地方放了什么,以便在列表中显示所有内容?谢谢

3 个解决方案

#1


1  

try something like below:

尝试类似下面的事情:

 <?php
    $db = (INFO HERE :));    
    $sql = "SELECT * FROM todolist";
    $result = mysqli_query($db, $sql);
    if(mysqli_num_rows($result) == 0) {
        echo "There is nothing else to do! :)";
    } 
    else{
        ?>
        <ul>
            <?php
            while($row = mysqli_fetch_assoc($result)){
                ?>
                  <li><?php echo $row["yourColumnName"] ?></li><?php                
            }?>
            </ul><?php
        }

?>

?>

#2


1  

$sql = "SELECT * FROM todolist";
$result = mysqli_query($db, $sql);
if (mysqli_num_rows($result) == 0) {
   echo "There is nothing else to do! :)";
   exit;
}

while ($row = mysql_fetch_assoc($result)) {
  // To see all data
       // print_r($row);
  //  to print single column value 
      //echo $row['id];
}

#3


1  

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    //Do something here  with $row array , for example print_r
print_r($row);
}

#1


1  

try something like below:

尝试类似下面的事情:

 <?php
    $db = (INFO HERE :));    
    $sql = "SELECT * FROM todolist";
    $result = mysqli_query($db, $sql);
    if(mysqli_num_rows($result) == 0) {
        echo "There is nothing else to do! :)";
    } 
    else{
        ?>
        <ul>
            <?php
            while($row = mysqli_fetch_assoc($result)){
                ?>
                  <li><?php echo $row["yourColumnName"] ?></li><?php                
            }?>
            </ul><?php
        }

?>

?>

#2


1  

$sql = "SELECT * FROM todolist";
$result = mysqli_query($db, $sql);
if (mysqli_num_rows($result) == 0) {
   echo "There is nothing else to do! :)";
   exit;
}

while ($row = mysql_fetch_assoc($result)) {
  // To see all data
       // print_r($row);
  //  to print single column value 
      //echo $row['id];
}

#3


1  

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    //Do something here  with $row array , for example print_r
print_r($row);
}