drjltlm156790 2017-07-14 11:51
浏览 81

使用while循环从SQL数据库php dpo显示数据到html表不起作用

$result = $heidisql->prepare($sql);
$result->execute();

while($users = $result->fetch(PDO::FETCH_ASSOC)) {
     $db_userid = $users["id"];
     $db_user_firstname = $users["firstname"];
     $db_user_lastname = $users["lastname"];
     $db_user_username = $users ["username"];
     $db_email_address = $users["emailaddress"];
     $db_postal_address = $users ["postal_address"];
     $db_postal_code = $users["user_postal_code"];
     $db_gender_id = $users["gender_id"];
     $db_date_of_birth = $users["date_of_birth"];
     $db_telephone = $users["userlast_login_time"];
     $db_activation_account = $users["act_token_time"];
     $db_last_logintime = $users["userlast_login_time"];

     if ($users > 0) {

        $tfirst="<table id='example' class='display table table-striped table-bordered' >";

        $theadcoreFirst="<thead><tr><th>";
        $theadcoreMiddle="</th><th>";
        $theadcoreLast="</th></tr></thead>";

        $tbodycoreFirst="<tbody><tr><td>";
        $tbodycoreMiddle="</td><td>";
        $tbodycoreLast="</td></tr></tbody>";

        $tlast="</table>";                      

        $htmlPHP_table = $tfirst. $theadcoreFirst."User ID" .$theadcoreMiddle. "First Name" .$theadcoreMiddle. "Last Name" .$theadcoreMiddle. "User Name" .$theadcoreMiddle
                                                 . "Email Address" .$theadcoreMiddle. "Postal Address" .$theadcoreMiddle. "Postal Code" .$theadcoreMiddle
                                                 . "Gender" .$theadcoreMiddle. "Date of Birth" .$theadcoreMiddle. "Phone No" .$theadcoreMiddle
                                                 . "Active Account Time" .$theadcoreMiddle. "Last Login Time" .$theadcoreLast;

       // Parse the result set, and adds each row and colums in HTML table
       $htmlPHP_table .= $tbodycoreFirst .$db_userid. $tbodycoreMiddle
                                                 .$db_user_firstname .$tbodycoreMiddle. $db_user_lastname .$tbodycoreMiddle. $db_user_username. $tbodycoreMiddle
                                                 .$db_email_address .$tbodycoreMiddle. $db_postal_address. $tbodycoreMiddle. $db_postal_code. $tbodycoreMiddle
                                                 .$db_gender_id .$tbodycoreMiddle .$db_date_of_birth .$tbodycoreMiddle. $db_telephone. $tbodycoreMiddle
                                                 .$db_activation_account .$tbodycoreMiddle. $db_last_logintime .$tbodycoreLast;

    $htmlPHP_table .= $tlast; // ends the HTML table

     echo $htmlPHP_table;

     }


   }

I am getting the results from my db but they appear like this

1)firstname|lastname|username|emailaddress|postal_address|user_postal_code|gender_id|date_of_birth|etc.

  • Then only 1 row of the data here!

2)firstname|lastname|username|emailaddress|postal_address|user_postal_code|gender_id|date_of_birth|etc.

  • Then only 1 row of the data here!

3)firstname|lastname|username|emailaddress|postal_address|user_postal_code|gender_id|date_of_birth|etc.

  • Then only 1 row of the data here, etc

Instead of:

firstname|lastname|username|emailaddress|postal_address|user_postal_code|gender_id|date_of_birth|etc

  • All ROWS and Columns data should appear here!?

  • Oh also how to limit the number of column to be display (e.g if I only want 10 out of 100 result to be display)?

  • 写回答

1条回答 默认 最新

  • dongpan1416 2017-07-14 12:13
    关注

    You need to put table starting and headings outside while loop. Only data to iterate will be in while loop

    $result = $heidisql->prepare($sql);
    $result->execute();
    if ($users > 0) {
    
        $tfirst="<table id='example' class='display table table-striped table-bordered' >";
    
        $theadcoreFirst="<thead><tr><th>";
        $theadcoreMiddle="</th><th>";
        $theadcoreLast="</th></tr></thead>";
    
        $tbodycoreFirst="<tbody><tr><td>";
        $tbodycoreMiddle="</td><td>";
        $tbodycoreLast="</td></tr></tbody>";
    
        $tlast="</table>";                      
    
        $htmlPHP_table = $tfirst. $theadcoreFirst."User ID" .$theadcoreMiddle. "First Name" .$theadcoreMiddle. "Last Name" .$theadcoreMiddle. "User Name" .$theadcoreMiddle
         . "Email Address" .$theadcoreMiddle. "Postal Address" .$theadcoreMiddle. "Postal Code" .$theadcoreMiddle
         . "Gender" .$theadcoreMiddle. "Date of Birth" .$theadcoreMiddle. "Phone No" .$theadcoreMiddle
         . "Active Account Time" .$theadcoreMiddle. "Last Login Time" .$theadcoreLast;
    while($users = $result->fetch(PDO::FETCH_ASSOC)) {
         $db_userid = $users["id"];
         $db_user_firstname = $users["firstname"];
         $db_user_lastname = $users["lastname"];
         $db_user_username = $users ["username"];
         $db_email_address = $users["emailaddress"];
         $db_postal_address = $users ["postal_address"];
         $db_postal_code = $users["user_postal_code"];
         $db_gender_id = $users["gender_id"];
         $db_date_of_birth = $users["date_of_birth"];
         $db_telephone = $users["userlast_login_time"];
         $db_activation_account = $users["act_token_time"];
         $db_last_logintime = $users["userlast_login_time"];
    
    
    
           // Parse the result set, and adds each row and colums in HTML table
           $htmlPHP_table .= $tbodycoreFirst .$db_userid. $tbodycoreMiddle
                                                     .$db_user_firstname .$tbodycoreMiddle. $db_user_lastname .$tbodycoreMiddle. $db_user_username. $tbodycoreMiddle
                                                     .$db_email_address .$tbodycoreMiddle. $db_postal_address. $tbodycoreMiddle. $db_postal_code. $tbodycoreMiddle
                                                     .$db_gender_id .$tbodycoreMiddle .$db_date_of_birth .$tbodycoreMiddle. $db_telephone. $tbodycoreMiddle
                                                     .$db_activation_account .$tbodycoreMiddle. $db_last_logintime .$tbodycoreLast;
    
    
    
         }
    
         $htmlPHP_table .= $tlast; // ends the HTML table
    
         echo $htmlPHP_table;
       }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题