简体   繁体   中英

PHP+MySQL Printing a MySQL array into table

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:

在此处输入图片说明

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>\n\t\t<td class=\"debug_nr\">".$this->_queryCount++."</td>\n\t\t<td class=\"debug_queInfo\"><b>Query: (".@mysql_num_rows($rs).")</b></td>\n\t\t<td>" . htmlspecialchars($querytext) . "</td>\n\t</tr>\n");
            if(mysql_error() != '') {
                $this->addDebugMessage("\t<tr>\n\t\t<td class=\"debug_nr\">".$this->_queryCount++."</td>\n\t\t<td class=\"debug_queInfo\"><b>Error #".mysql_errno($this->_link)." :</b></td>\n\t\t<td>" . htmlspecialchars(mysql_error($this->_link)) . "</td>\n\t</tr>\n");
            }
        }
        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;
        }
    }

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

You could loop through the field names, too, something like this:

<?php
// replace with your own array from the DB query
$visas = array(
        array(  'name' => 'John',
            'age' => '43'),
        array( 'name' => 'Sally',
            'age' => '36')
    );
echo "Visas<br/><table border='1'>";
foreach($visas as $visa) {
    echo "<tr>";
    foreach($visa as $key => $value) {
        echo "<td><em>$key:</em></td><td>$value</td>";
    }
    echo "</tr>";
}
echo "<table>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM