0

我的脚本中有这段代码,它只显示第一行..有什么解决方案吗?(这不是我的全部剧本)

    $sql = mysql_query ('select * from todo');
if (!$sql) {
    die('Invalid query: ' . mysql_error());
}
$taskrow = mysql_query ('select * from todo');
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $taak = $_POST['taak'];
    $beschrijving = $_POST['beschrijving'];
    $categorie = $_POST['categorie'];
    $prioriteit = $_POST['prioriteit'];
    $datum = $_POST['datum'];
    $query = mysql_query("INSERT INTO todo (taak, beschrijving, categorie, prioriteit, datum) VALUES ('$taak', '$beschrijving', '$categorie', '$prioriteit', '$datum')") or die(mysql_error());
 } ?>

<?php $task = mysql_fetch_assoc($taskrow); ?>

<td><?php echo $task['taak']; ?></td>
<td><?php echo $task['beschrijving']; ?></td>
<td><?php echo $task['categorie']; ?></td>
<td><?php echo $task['prioriteit']; ?></td>
<td><?php echo $task['datum']; ?> </td>    
4

2 回答 2

6

通过做:

$task = mysql_fetch_assoc($taskrow);

您正在$task变成一个对象,其中包含数据库查询中所选行的数据。

因此,您需要遍历该对象,以便使用每一行的数据...

所以试试:

while ($task = mysql_fetch_assoc($taskrow)){?>

<td><?php echo $task['taak'];?></td>
<td><?php echo $task['beschrijving']; ?> </td>
<td><?php echo $task['categorie']; ?></td>
<td><?php echo $task['prioriteit']; ?></td>
<td><?php echo $task['datum']; ?> </td>  
<? } ?>
于 2012-04-19T09:20:33.030 回答
0
<?php foreach($task as $key => $value) : ?>
<td><?php echo $value;?></td>
<?php endforeach; ?>
于 2012-04-19T09:23:43.713 回答