我正在尝试使用 php 和 mysql 从我的数据库中检索 fata。 我已经设法从表的第一行获取数据,但我似乎找不到方法让它到达第二行,依此类推。
我的数据库是这样的:
(表格第一行)
id [int] -> 7
user_id [int] -> 23
user_task_id [int] -> 4
description [varchar] -> Text Text Text Text Text
date_creation [timestamp] -> 2013-07-17
date_end [datetime] -> 0000-00-00
date_conclusion [datetime] -> 0000-00-00
status_id [int] -> 3
notes [text] -> Text Text Text Text Text Text
categoria_id [int] -> 12
entity_id [int] -> 17
(表格第二行)
id [int] -> 8
user_id [int] -> 6
user_task_id [int] -> 9
description [varchar] -> Text Text Text Text Text
date_creation [timestamp] -> 2013-07-20
date_end [datetime] -> 0000-00-00
date_conclusion [datetime] -> 0000-00-00
status_id [int] -> 4
notes [text] -> Text Text Text Text Text Text
categoria_id [int] -> 18
entity_id [int] -> 10
代码如下:
<?php
function get_all_tasks() {
global $connection;
$query = "SELECT * FROM tarefas;";
$tasks_set = mysql_query($query, $connection);
confirm_query($tasks_set);
return $tasks_set;
}
?>
<table class="table_content">
<thead>
<tr>
<td>ID</td>
<td>User Task</td>
<td>Description</td>
<td>Entity ID</td>
<td>Date End</td>
<td>Status</td>
<td>Actions</td>
</tr>
</thead>
<?php
?>
<tbody>
<tr>
<?php
if($task_data = mysql_fetch_array(get_all_tasks())){
foreach ($task_data as $task) { ?>
<tr>
<td><?php echo $task['id'] ?></td>
<td><?php echo $task['user_task_id'] ?></td>
<td><?php echo $task['description'] ?></td>
<td><?php echo $task['entity_id'] ?></td>
<td><?php echo $task['date_end'] ?></td>
<td><?php echo $task['status_id'] ?></td>
<td><?php echo "" ?></td>
</tr>
<?php }}
?>
</tbody>
</table>
最佳答案
尝试使用这个:
while ($task = mysql_fetch_array(get_all_tasks()) { your html }
请记住,mysql_* 已弃用,最好使用 mysqli 或 PDO。
关于php - Foreach 从 MySQL 检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17763365/