从数据库获取数据和安排如表

时间:2016-03-26 06:04:38

标签: php html mysql database

我有这段代码:

<?php
$dbhost = 'localhost';
$dbuser = 'MyUsername';
$dbpass = 'MyPassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT contactID, name, phone, email, choise, message
        FROM contact2';

mysql_select_db('nghiartc_contact');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}

while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{

    echo "<td>{$row['contactID']} </td><td>{$row['name']}</td><td>  {$row['phone']} </td><td>{$row['email']}</td> <td>{$row['choise']}</td> <td>{$row['message']}</td>";        
} 

mysql_close($conn);
?>

我正试图像这样显示数据库中的数据:

enter image description here

但是现在我得到了:

enter image description here

知道我需要如何修改代码,所以我得到了预期的输出?

3 个答案:

答案 0 :(得分:0)

您缺少表结构iteslf - 尝试添加<table><tr>标记。然后你可以用边框等对它进行相应的设计。

echo"<table>";
//echo"<tr><th>...table header structure...</th></tr>";

while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    echo "<tr><td>{$row['contactID']} </td><td>{$row['name']}</td><td>  {$row['phone']} </td><td>{$row['email']}</td> <td>{$row['choise']}</td> <td>{$row['message']}</td></tr>";        
} 
echo"</table>";

答案 1 :(得分:0)

请注意您没有遵循正确的HTML结构。你应该使用和标签。

例如,

echo "<table border='1'>";

while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
   echo "<tr>";
   echo "<td>{$row['contactID']} </td><td>{$row['name']}</td><td>  {$row['phone']} </td><td>{$row['email']}</td> <td>{$row['choise']}</td> <td>{$row['message']}</td>"; 
   echo "</tr>";      
} 
echo "</table>";

希望这有帮助。

答案 2 :(得分:0)

您缺少启动表格的table代码和每行数据的tr代码,因此您必须将while循环修改为:

echo "<table>";
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{

    echo "<tr>";
    echo "<td>{$row['contactID']}</td>
          <td>{$row['name']}</td>
          <td>{$row['phone']}</td>
          <td>{$row['email']}</td>
          <td>{$row['choise']}</td>
          <td>{$row['message']}</td>";    
    echo "</tr>";    
} 
echo "</table>";

But please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?请改为了解prepared statements,并使用PDOMySQLi

所以我强烈建议您将代码更改为PDO,请阅读手册中的更多内容,例如:

<?php

    $dbhost = 'localhost';
    $dbuser = 'nghiartc_contact';
    $dbpass = 'ZP5IEVeyKx';

    try {

        $dbh = new PDO("mysql:host=$dbhost;dbname=nghiartc_contact", $dbuser, $dbpass);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $result = $dbh->query("SELECT contactID, name, phone, email, choise, message FROM contact2");

    } catch(PDOException $e) {
        echo $e->getMessage();
    }


    echo "<table>";
    foreach($result->fetchAll() as $row){
        echo "<tr>";
            echo "<td>" . $row["contactID"] . "</td>";
            echo "<td>" . $row["name"] . "</td>";
            echo "<td>" . $row["phone"] . "</td>";
            echo "<td>" . $row["email"] . "</td>";
            echo "<td>" . $row["choise"] . "</td>";
            echo "<td>" . $row["message"] . "</td>";
        echo "</tr>";
    }
    echo "</table>";

    $dbh = NULL;

?>