Foreach从MySQL检索数据

时间:2013-07-20 14:44:33

标签: 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>

4 个答案:

答案 0 :(得分:0)

尝试使用这个:

  

while($ task = mysql_fetch_array(get_all_tasks()){your html}

请记住,不推荐使用mysql_ *,最好使用mysqli或PDO。

答案 1 :(得分:0)

试试这个

    <?php
  function get_all_tasks() {
    global $connection;
    $query = "SELECT * FROM tarefas;";
    $tasks_set = mysql_query($query, $connection);
    confirm_query($tasks_set);
      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 }

}
 ?>


  <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 
                       echo get_all_tasks() ;
                    ?>
                </tbody>
            </table>

注意:不推荐使用mysql_ *,最好使用mysqli或PDO

答案 2 :(得分:0)

将调用移到while循环之外的函数。并编写如下代码

<?php 
   $data = get_all_tasks();
   while (($task = mysql_fetch_array($data))) {
                <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 ""

答案 3 :(得分:0)

只需用以下内容更新您的get_all_task功能:

<?php
function get_all_tasks() {
        global $connection;
        $query = "SELECT * FROM tarefas;";
        $tasks_set1 = mysql_query($query, $connection);
       while ($task_set = mysql_fetch_array($tasks_set1, MYSQL_ASSOC))
        return $tasks_set;
}
?>