php - Foreach 从 MySQL 检索数据

标签 php mysql

我正在尝试使用 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/

相关文章:

php - 如何在我的表中获取最后一个 reasonID?

PHP MySQL 用户最近 10 分钟在线

php - MYSQL 查询无法正常工作

mysql - 两个内部连接在一个sql语句中

php - session 未开始? + 奇怪的错误消息

php - php错误传递给google map api的地址坐标

php - Smarty in_array 值

php - 授予用户对特定 mySQL 表的权限是一种良好的安全做法吗?

mysql根据另一个选择选择列的值

MySQL : Using a function output with IN