pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  1. <tfoot id='joRCW'></tfoot>
    1. <legend id='joRCW'><style id='joRCW'><dir id='joRCW'><q id='joRCW'></q></dir></style></legend>

      <small id='joRCW'></small><noframes id='joRCW'>

    2. <i id='joRCW'><tr id='joRCW'><dt id='joRCW'><q id='joRCW'><span id='joRCW'><b id='joRCW'><form id='joRCW'><ins id='joRCW'></ins><ul id='joRCW'></ul><sub id='joRCW'></sub></form><legend id='joRCW'></legend><bdo id='joRCW'><pre id='joRCW'><center id='joRCW'></center></pre></bdo></b><th id='joRCW'></th></span></q></dt></tr></i><div class="g0sscke" id='joRCW'><tfoot id='joRCW'></tfoot><dl id='joRCW'><fieldset id='joRCW'></fieldset></dl></div>
        <bdo id='joRCW'></bdo><ul id='joRCW'></ul>
    3. 使用 PHP 在 HTML 表格中顯示 MySQL 結果

      Use PHP to Display MySQL Results in HTML Table(使用 PHP 在 HTML 表格中顯示 MySQL 結果)

      <legend id='DhIjT'><style id='DhIjT'><dir id='DhIjT'><q id='DhIjT'></q></dir></style></legend>
      <i id='DhIjT'><tr id='DhIjT'><dt id='DhIjT'><q id='DhIjT'><span id='DhIjT'><b id='DhIjT'><form id='DhIjT'><ins id='DhIjT'></ins><ul id='DhIjT'></ul><sub id='DhIjT'></sub></form><legend id='DhIjT'></legend><bdo id='DhIjT'><pre id='DhIjT'><center id='DhIjT'></center></pre></bdo></b><th id='DhIjT'></th></span></q></dt></tr></i><div class="esmak2o" id='DhIjT'><tfoot id='DhIjT'></tfoot><dl id='DhIjT'><fieldset id='DhIjT'></fieldset></dl></div>

            <bdo id='DhIjT'></bdo><ul id='DhIjT'></ul>
            • <tfoot id='DhIjT'></tfoot>

                  <tbody id='DhIjT'></tbody>
              1. <small id='DhIjT'></small><noframes id='DhIjT'>

              2. 本文介紹了使用 PHP 在 HTML 表格中顯示 MySQL 結果的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                CodingBiz 更新:

                我把它放在我的代碼中:

                I'm putting this in my code:

                for($i=1;$i<=$numRows;$i++) {
                    $output .= '<tr>';
                    $row = $this->fetchAssoc($result);
                    $colRow = $this->fetchAssoc($colResult);
                    foreach($colRow as $colName) {
                        $output .= "<td>".$row[$colName]."</td>";
                    }
                    $output .= '</tr>';
                }
                

                代替

                for($i=1;$i<=$numRows;$i++) {
                    $output .= '<tr>';
                    $row = $this->fetchAssoc($result);
                    for($j=1;$j<=$colNumRows;$j++) {
                        $colRow = $this->fetchAssoc($colResult);
                        $output .= "<td>".$row[$colRow["COLUMN_NAME"]]."</td>";
                    }
                    $output .= '</tr>';
                }
                

                這有什么問題嗎?

                原帖:

                我正在 PHP 類中編寫一個函數來在表中顯示查詢結果.我沒有自己構建任何表格,我希望它使用 PHP 完成所有工作.到目前為止,這是我的代碼:

                I'm writing a function in a PHP class to display the results of a query in a table. I'm not structuring any of the table myself, I want it everything to be done using PHP. Here is my code so far:

                function allResults($table,$cols) {
                    if(isset($cols)) {
                        $query = "SELECT $cols FROM $table";
                    }
                    else {
                        $query = "SELECT * FROM $table";
                    }
                    $result = $this->query($query);
                    $numRows =  $this->numRows($result);
                    $colQuery ="SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='shareride'  AND TABLE_NAME='$table'";
                    $colResult = $this->query($colQuery);
                    $colNumRows = $this->numRows($colResult);
                
                    $output = '<table class="allResults">';
                    $output .= '<tr>';
                    for($i=1;$i<=$colNumRows;$i++) {
                        $colRow = $this->fetchAssoc($colResult);
                        $output .= "<td>".$colRow["COLUMN_NAME"]."</td>";
                    }
                    $output .= '</tr>';
                    for($i=1;$i<=$numRows;$i++) {
                        $output .= '<tr>';
                        $row = $this->fetchAssoc($result);
                        for($j=1;$j<=$colNumRows;$j++) {
                            $colRow = $this->fetchAssoc($colResult);
                            $output .= "<td>".$row[$colRow["COLUMN_NAME"]]."</td>";
                        }
                        $output .= '</tr>';
                    }
                    $output .= '</table>';
                    return $output;
                }
                

                如果不清楚,query指的是mysqli_querynumRows指的是mysqli_num_rows,>fetchAssoc 指的是 mysqli_fetch_assoc.數據庫名稱是shareride".

                In case it is unclear, query refers to mysqli_query, numRows refers to mysqli_num_rows, and fetchAssoc refers to mysqli_fetch_assoc. The database name is "shareride."

                我知道我在這一行中遺漏了一些東西:

                I know I am missing something in this line:

                $output .= "<td>".$row[$colRow["COLUMN_NAME"]]."</td>";
                

                但我只是不知道它是什么.現在,我正確顯示了所有表格列標題,并獲得了正確數量的內容行,但我無法使用數據庫中的實際數據填充這些行.

                but I just don't know what it is. Right now, I get all the table column titles displayed correctly, and I get the correct number of content rows, but I just can't populate those rows with the actual data from the database.

                我錯過了什么?任何幫助將非常感激!

                What am I missing? Any help would be GREATLY appreciated!

                推薦答案

                從同一個結果集中獲取數據和列名

                Get the data and column names from the same result set

                  <?php
                  $i = 0;
                  $colNames = array();
                  $data = array();
                  while($row = ***_fetch_assoc($res)) //where $res is from the main query result not schema information
                  {
                     //get the column names into an array $colNames
                     if($i == 0) //make sure this is done once
                     {
                        foreach($row as $colname => $val)
                           $colNames[] = $colname;
                     }
                
                     //get the data into an array
                     $data[] = $row;
                
                     $i++;
                  }
                
                 ?>
                

                更新:@YourCommonSense 建議替換上面的代碼,它有效,簡單且更短 - 一種無需像我那樣循環即可獲取列名/數組鍵的方法

                  $data = array();
                  while($row = mysql_fetch_assoc($res))
                  {
                     $data[] = $row;
                  }
                
                  $colNames = array_keys(reset($data))
                

                繼續:打印表格

                 <table border="1">
                 <tr>
                    <?php
                       //print the header
                       foreach($colNames as $colName)
                       {
                          echo "<th>$colName</th>";
                       }
                    ?>
                 </tr>
                
                    <?php
                       //print the rows
                       foreach($data as $row)
                       {
                          echo "<tr>";
                          foreach($colNames as $colName)
                          {
                             echo "<td>".$row[$colName]."</td>";
                          }
                          echo "</tr>";
                       }
                    ?>
                 </table>
                

                測試結果

                您可以看到我如何將數據檢索與表生成分離.它們現在相互依賴,您可以通過使用靜態數據填充數組來測試沒有數據庫的表生成

                You can see how I separated the data retrieval from table generation. They are dependent of each other now and you can test your table generation without the database by populating the arrays with static data

                你也可以把它們做成單獨的函數.

                You can also make them into separate functions.

                這篇關于使用 PHP 在 HTML 表格中顯示 MySQL 結果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                store_result() and get_result() in mysql returns false(mysql 中的 store_result() 和 get_result() 返回 false)
                Call to undefined function mysqli_result::num_rows()(調用未定義的函數 mysqli_result::num_rows())
                PHP Prepared Statement Problems(PHP 準備好的語句問題)
                mysqli_fetch_array returning only one result(mysqli_fetch_array 只返回一個結果)
                PHP MySQLi Multiple Inserts(PHP MySQLi 多次插入)
                How do I make sure that values from MySQL keep their type in PHP?(如何確保 MySQL 中的值在 PHP 中保持其類型?)
                <i id='iZJo8'><tr id='iZJo8'><dt id='iZJo8'><q id='iZJo8'><span id='iZJo8'><b id='iZJo8'><form id='iZJo8'><ins id='iZJo8'></ins><ul id='iZJo8'></ul><sub id='iZJo8'></sub></form><legend id='iZJo8'></legend><bdo id='iZJo8'><pre id='iZJo8'><center id='iZJo8'></center></pre></bdo></b><th id='iZJo8'></th></span></q></dt></tr></i><div class="umgyeac" id='iZJo8'><tfoot id='iZJo8'></tfoot><dl id='iZJo8'><fieldset id='iZJo8'></fieldset></dl></div>
                  • <small id='iZJo8'></small><noframes id='iZJo8'>

                  • <tfoot id='iZJo8'></tfoot>
                    1. <legend id='iZJo8'><style id='iZJo8'><dir id='iZJo8'><q id='iZJo8'></q></dir></style></legend>
                        <bdo id='iZJo8'></bdo><ul id='iZJo8'></ul>
                            <tbody id='iZJo8'></tbody>

                          主站蜘蛛池模板: AR开发公司_AR增强现实_AR工业_AR巡检|上海集英科技 | 首页|专注深圳注册公司,代理记账报税,注册商标代理,工商变更,企业400电话等企业一站式服务-慧用心 | 杭州实验室尾气处理_实验台_实验室家具_杭州秋叶实验设备有限公司 | 提升海外网站流量,增加国外网站访客UV,定制海外IP-访客王 | 软文发布-新闻发布推广平台-代写文章-网络广告营销-自助发稿公司媒介星 | 【铜排折弯机,钢丝折弯成型机,汽车发泡钢丝折弯机,线材折弯机厂家,线材成型机,铁线折弯机】贝朗折弯机厂家_东莞市贝朗自动化设备有限公司 | 不锈钢轴流风机,不锈钢电机-许昌光维防爆电机有限公司(原许昌光维特种电机技术有限公司) | 交通信号灯生产厂家_红绿灯厂家_电子警察监控杆_标志杆厂家-沃霖电子科技 | 质构仪_鱼糜弹性仪-上海腾拔仪器科技有限公司 | 点焊机-缝焊机-闪光对焊机-电阻焊设备生产厂家-上海骏腾发智能设备有限公司 | 昆明网络公司|云南网络公司|昆明网站建设公司|昆明网页设计|云南网站制作|新媒体运营公司|APP开发|小程序研发|尽在昆明奥远科技有限公司 | 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 外贮压-柜式-悬挂式-七氟丙烷-灭火器-灭火系统-药剂-价格-厂家-IG541-混合气体-贮压-非贮压-超细干粉-自动-灭火装置-气体灭火设备-探火管灭火厂家-东莞汇建消防科技有限公司 | 自动记录数据电子台秤,记忆储存重量电子桌称,设定时间记录电子秤-昆山巨天 | PAS糖原染色-CBA流式多因子-明胶酶谱MMP-上海研谨生物科技有限公司 | 蔡司三坐标-影像测量机-3D扫描仪-蔡司显微镜-扫描电镜-工业CT-ZEISS授权代理商三本工业测量 | 期货软件-专业期货分析软件下载-云智赢 | 湖南自考_湖南自学考试| 玉米深加工设备-玉米深加工机械-新型玉米工机械生产厂家-河南粮院机械制造有限公司 | 太原装修公司_山西整装家装设计_太原室内装潢软装_肖邦家居 | 出国劳务公司_正规派遣公司[严海]| 口臭的治疗方法,口臭怎么办,怎么除口臭,口臭的原因-口臭治疗网 | 石家庄小程序开发_小程序开发公司_APP开发_网站制作-石家庄乘航网络科技有限公司 | 国产液相色谱仪-超高效液相色谱仪厂家-上海伍丰科学仪器有限公司 | 福州仿石漆加盟_福建仿石漆厂家-外墙仿石漆加盟推荐铁壁金钢(福建)新材料科技有限公司有保障 | 包装机传感器-搅拌站传感器-山东称重传感器厂家-济南泰钦电气 | 打孔器,打孔钳厂家【温州新星德牌五金工具】 | 衬氟止回阀_衬氟闸阀_衬氟三通球阀_衬四氟阀门_衬氟阀门厂-浙江利尔多阀门有限公司 | 高柔性拖链电缆-聚氨酯卷筒电缆-柔性屏蔽电缆厂家-玖泰电缆 | 设计圈 - 让设计更有价值!| 真空搅拌机-行星搅拌机-双行星动力混合机-广州市番禺区源创化工设备厂 | 软文发布平台 - 云软媒网络软文直编发布营销推广平台 | 隧道风机_DWEX边墙风机_SDS射流风机-绍兴市上虞科瑞风机有限公司 | 无线讲解器-导游讲解器-自助讲解器-分区讲解系统 品牌生产厂家[鹰米讲解-合肥市徽马信息科技有限公司] | 过跨车_过跨电瓶车_过跨转运车_横移电动平车_厂区转运车_无轨转运车 | 韦伯电梯有限公司| 双段式高压鼓风机-雕刻机用真空泵-绍兴天晨机械有限公司 | 不锈钢/气体/液体玻璃转子流量计(防腐,选型,规格)-常州天晟热工仪表有限公司【官网】 | 连续油炸机,全自动油炸机,花生米油炸机-烟台茂源食品机械制造有限公司 | 考勤系统_人事考勤管理系统_本地部署BS考勤系统_考勤软件_天时考勤管理专家 | 变色龙云 - 打包app_原生app_在线制作平台_短链接_ip查询 |