-1

我有这段代码可以从数据库中选择和输出数据

<?php

require('system/connect.php'); //load the connection file

$sql = ("SELECT * FROM `movie`"); // add mysql code to a variable. In this case it will    select ALL columns from the database.

$query = mysql_query($sql); //run the query contained within the variable.

while ($row = mysql_fetch_array($query)) { //store each single row from the database in an array named $row. While there are any rows left, loop through and execute the following code:

$id = $row['movie_id']; //gets name from DB for a single row
$name = $row['movie_name']; //gets age from DB for a single row
$category = $row['movie_category']; //gets age from DB for a single row

//Following code outputs the data to the webpage:

echo $id;

echo $name;

echo $category;
};
?>

页面显示:1titanicromance2zoroaction3blood diamondsaction

我需要一种方法来制作表格或数组并将数据直接插入其中。

4

2 回答 2

1

为 Table 添加 HTML 应该可以解决问题。虽然,混合 PHP 和 HTML 是一种糟糕的编码。

<?php

require('system/connect.php'); //load the connection file

$sql = ("SELECT * FROM `movie`"); // add mysql code to a variable. In this case it will    select ALL columns from the database.

$query = mysql_query($sql); //run the query contained within the variable.

echo '<table>';

while ($row = mysql_fetch_array($query)) { //store each single row from the database in an array named $row. While there are any rows left, loop through and execute the following code:

$id = $row['movie_id']; //gets name from DB for a single row
$name = $row['movie_name']; //gets age from DB for a single row
$category = $row['movie_category']; //gets age from DB for a single row

//Following code outputs the data to the webpage:
echo '<tr>';

echo '<td>' . $id . '</td>';

echo '<td>' . $name . '</td>';

echo '<td>' . $category . '</td>';

echo '</tr>';

};

echo '</table>';

?>
于 2013-03-27T18:00:10.243 回答
0

你的意思是HTML表格吗?然后它看起来像这样

<?php

require('system/connect.php'); //load the connection file

$sql = ("SELECT * FROM `movie`"); // add mysql code to a variable. In this case it will    select ALL columns from the database.

$query = mysql_query($sql); //run the query contained within the variable.

if (mysql_num_rows($query)) { // if there is some rows in result
    echo "<table>"; // starting HTML table
    while ($row = mysql_fetch_array($query)) { //loop through the result                           
       echo "<tr>".
                "<td>".$row['movie_id']."</td>". 
                "<td>".$row['movie_name']."</td>".
                "<td>".$row['movie_category']."</td>".
            "</tr>"; 
    }
    echo "</table>";// finishing HTML table
}
?>

注意:不要使用mysql_*函数。它们已被弃用。请改用PDOMysqli

于 2013-03-27T18:00:34.823 回答