打印[mysql_query]结果表

时间:2011-08-08 11:44:49

标签: php mysql sql

我想在表中打印mysql_query结果。我知道怎么做但我只是困惑。我试过这个。

<?php
mysql_connect("localhost","root","") or die("Could not Connect.");
mysql_select_db("check") or die("Could not Select DB");
$table = "cc";
$i = 1;
$query = "select * from $table";
$sql = mysql_query($query);
if($sql){
echo "<table border='5'><tr>";
while($i<=2 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " .  $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=4 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " .  $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=6 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " .  $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=8 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " .  $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
echo "</tr></table>";
}

?> 

正如你所看到的那样,在while循环中稍微改变2,4,6,8会一次又一次地写入。它的工作原理但问题是我不能一次又一次地重写它,因为当网站上线时它将有超过1000个条目。你们可以建议另一种方法来帮助我吗?


“” **我需要它像这些点(点代表数据库中的记录)** “”“

                        . . . .

                        . . . .

                        . . . .

感谢提前。 RAMZY

6 个答案:

答案 0 :(得分:0)

<?php
    mysql_connect("localhost","root","") or die("Could not Connect.");
    mysql_select_db("check") or die("Could not Select DB");
    $table = "cc";
    $query = "select * from $table";
    $sql = mysql_query($query);

    if($sql){
        echo "<table border='5'><tr>";
        while($row = mysql_fetch_array($sql)){
            echo "<td>" . $row['id'] . " : " .  $row['name'] . "</td>";
        }
        echo "</tr></table>";
    }
?>

答案 1 :(得分:0)

while($row = mysql_fetch_array($sql)) {
    echo "<td>" . $row['id'] . " : " .  $row['name'] . "</td>";
}

我真的不明白这里有什么问题。 顺便说一句,你永远不应该称你为像$row[id]这样的数组,但是你应该引用密钥$row['id'];因为如果存在常量id,它会搞砸你的代码,也是出于性能原因。

答案 2 :(得分:0)

只需使用

$limit = 1000;//place any value you need here to limit the number of rows displayed
while ($i<=$limit && $row = mysql_fetch_array($sql)){
    echo "<td>" . $row['id'] . " : " .  $row['name'] . "</td>";
    ++$i;
}

此外,如果您只想将每条记录刷新到输出,则不需要该限制。你可以做到

while ($row = mysql_fetch_array($sql)){
    echo "<td>" . $row['id'] . " : " .  $row['name'] . "</td>";
}

一旦没有更多记录,它就会停止。

答案 3 :(得分:0)

要将所有数据库行打印到HTML表中,请使用:

echo '<table>';
$i = 0; // $i is just for numbering the output, not really useful
while($row = mysql_fetch_array($sql))
{
    echo '<tr><td>' . $i . ' - ' . $row['id'] . ' : ' . $row['name'] . '</td></tr>';
    $i++;
}
echo '</tr></table>';

答案 4 :(得分:0)

这是我使用的一般功能:

function query_result_to_html_table($res, $table_id = NULL, $table_class = NULL, $display_null = true)
{
    $table = array();
    while ($tmp = mysql_fetch_assoc($res))
            array_push($table, $tmp);

    if (!count($table))
            return false;

    echo "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" "
                    . ($table_id ? "id=\"$table_id\" " : "")
                    . ($table_class ? "class=\"$table_class\" " : "") . ">";
    echo "<tr>";
    foreach (array_keys($table[0]) as $field_name) {
            echo "<th>$field_name";
    }

    foreach ($table as $row) {
            echo "<tr>";
            foreach ($row as $col => $value) {
                    echo "<td>";
                    if ($value === NULL)
                            echo "NULL";
                    else
                            echo $value;
            }
            echo "\n";
    }
    echo "</table>\n";
    return true;
}

答案 5 :(得分:0)

我有答案..我希望它是这样的。我做了这个,它实际上有效。


<?php
$i = 1;
mysql_connect("localhost" , "root" , "") or die('Could not Connect.');
mysql_select_db("db") or die('Could not select DB.');
$query = "select * from `check`";
$sql = mysql_query($query) or die(mysql_error());
echo "<table border='5' width='50%'><tr><th>Name</th><th>Gender</th></tr></table><table border='5' width='50%'><tr>";
if($i<3){
echo "<td align='center'>".$row['name']."</td>";
echo "<td align='center'>".$row['gender']."</td>";
++$i;
} else {
echo "<td align='center'>".$row['name']."</td><td align='center'>".$row['gender']."</td>";
echo "</tr>";
$i = 1;
echo "<tr>";
}
}
echo "</table>";
?>
</div>

感谢你们的支持