douti0467 2014-05-31 22:01
浏览 29
已采纳

PHP + MySQL将MySQL数组打印到表中

I have a query function (code below) that I use to read the DB and I would like to know how do I loop the print to output -ALL- the results into a table which is something like this:

enter image description here

I used this to get it:

$visa=$DB->query("SELECT fname,lname,email FROM users");
    echo '<table BORDERCOLOR=black>';
   ?>
    <tr>
        <th>Name</th><th>LastName</th><th>E-Mail</th>
    </tr>
    <?php
    echo "<td>";
    echo $visa[0]['fname'];
    echo "</td>";
    echo "<td>";
    echo $visa[0]['lname'];
    echo "</td>";
    echo "<td>";
    echo $visa[0]['email'];
    echo "</td>";

The query function:

function query($querytext) {
        $rs = mysql_query($querytext, $this->_link);
        if($this->_debug) {
            $this->addDebugMessage("\t<tr>
\t\t<td class=\"debug_nr\">".$this->_queryCount++."</td>
\t\t<td class=\"debug_queInfo\"><b>Query: (".@mysql_num_rows($rs).")</b></td>
\t\t<td>" . htmlspecialchars($querytext) . "</td>
\t</tr>
");
            if(mysql_error() != '') {
                $this->addDebugMessage("\t<tr>
\t\t<td class=\"debug_nr\">".$this->_queryCount++."</td>
\t\t<td class=\"debug_queInfo\"><b>Error #".mysql_errno($this->_link)." :</b></td>
\t\t<td>" . htmlspecialchars(mysql_error($this->_link)) . "</td>
\t</tr>
");
            }
        }
        if($rs) {
            $num_rows = @mysql_num_rows($rs);
            if($num_rows) {
                if($num_rows > 0) {
                    $rsarray = array();
                    while($line = mysql_fetch_array($rs , MYSQL_ASSOC)) {
                        array_push($rsarray, $line);
                    }
                    mysql_free_result($rs);
                    return $rsarray;
                } else {
                    return false;
                }
            } else {
                if(mysql_affected_rows($this->_link) > 0) {
                    return true;
                } else {
                    return false;
                }
            }
        } else {
            return false;
        }
    }
  • 写回答

2条回答 默认 最新

  • dongxuan1314 2014-05-31 22:04
    关注

    Use for or foreach loop:

    <?php foreach($visa as $v) {
    echo "<td>";
    echo $v['fname'];
    echo "</td>";
    echo "<td>";
    echo $v['lname'];
    echo "</td>";
    echo "<td>";
    echo $v['email'];
    echo "</td>";
    }
    

    In your code, $visa is an array of query results, and to display all the values form it you just have too loop over that array

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP