在PHP数组中显示数据库表

时间:2013-09-13 04:56:48

标签: php mysql database

我正在尝试从我的MySQL数据库中显示我的表。它似乎不起作用,我想这很可能是一件简单的事情。如果可能的话,我希望桌子看起来像一张没有多余装饰的普通桌子;只是一个边框,并能够适合正常大小的页面。这就是我到目前为止所做的:

<html>
<head>
<title>Profile Database </title>
</head>
<body>
<?
$connection = "connect.php";
require $connection;

mysql_select_db("bank");

$sql = "SELECT * FROM profiles";

$result = mysql_query($sql);
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>

<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
</thead>
<tbody>

 <?php

 while( $row = mysql_fetch_assoc( $result ))
 {
 echo <tr>
 <td>{$row\['id'\]}</td>
 <td>{$row\['fname'\]}</td>
 <td>{$row\['lname'\]}</td>
 <td>{$row\['email'\]}</td>
 <td>{$row\['dob'\]}</td>
 <td>{$row\['age'\]}</td>
 <td>{$row\['city'\]}</td> 
 <td>{$row\['state'\]}</td>
 <td>{$row\['zip'\]}</td>
 <td>{$row\['offence'\]}</td>
 <td>{$row\['notes'\]}</td>
 </tr>\n;
 }
 ?>
 </tbody>
 </table>

 <?php 
 mysql_close($connection); 
 ?>

 </body>
 </html>

5 个答案:

答案 0 :(得分:0)

放手一搏:

<table>
    <tr>
        <th>ID</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>E-Mail</th>
        <th>DOB</th>
        <th>Age</th>
        <th>City</th>
        <th>State</th>
        <th>Zip Code</th>
        <th>Offence</th>
        <th>Notes</th>
    </tr>
    <?php

        require "connect.php";

        mysql_select_db("bank");
        $result = mysql_query("SELECT * FROM profiles");

        while($row = mysql_fetch_array($result, MYSQL_NUM)) {
            echo '<tr>';
            foreach($row as $column) {
                echo '<td>', $column, '</td>';
            }
            echo '</tr>';
        }
    ?>
</table>

答案 1 :(得分:0)

WHILE($row = mysql_fetch_array($result))

应该是

WHILE($row = mysql_fetch_array($result, MYSQL_ASSOC))

OR

WHILE($row = mysql_fetch_assoc($result))

此外,不推荐使用mysql_ *函数,并且非常不鼓励使用它们。

请检查以下讨论: -

Deprecated MySql Functions
How to successfully rewrite old mysql-php code with deprecated mysql_* functions?
http://php.net/manual/en/migration55.deprecated.php

使用MySQLiPDO

答案 2 :(得分:0)

<center><table border="1">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
<?
$connection = "connect.php";
require $connection;

mysql_select_db("bank");

$sql = "SELECT * FROM profiles";

$result = mysql_query($sql);

WHILE($row = mysql_fetch_array($result))

{
$id = $row ['id'];
$firstname = $row ['fname'];
$lastname = $row ['lname'];
$email = $row ['email'];
$dob = $row ['dob'];
$age = $row ['age'];
$city = $row ['city'];
$state = $row ['state'];
$zip = $row ['zip'];
$offence = $row ['offence'];
$nots = $row ['notes'];

echo '<tr>';   // change here
echo '<td>'.$id.'</td>';
echo '<td>'.$firstname.'</td>';
echo '<td>'.$lastname.'</td>'; 
echo '<td>'.$email.'</td>';
echo '<td>'.$dob.'</td>'; 
echo '<td>'.$age.'</td>';
echo '<td>'.$city.'</td>'; 
echo '<td>'.$state.'</td>';
echo '<td>'.$zip.'</td>'; 
echo '<td>'.$offence.'</td>';
echo '<td>'.$notes.'</td>'; 
echo '</tr>';  // change here
}

mysql_close();
?>


</table></center>

答案 3 :(得分:0)

试试这段代码:

<?php

$username = "your_name";
$password = "your_password";
$hostname = "localhost";

$dbconnect = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$select=mysql_select_db("bank",$dbconnect) or die("Could not select bank");

$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
?>
<center><table border="1">
<tr>
<th>ID</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>E-Mail</th>
    <th>DOB</th>
    <th>Age</th>
    <th>City</th>
    <th>State</th>
    <th>Zip Code</th>
    <th>Offence</th>
    <th>Notes</th></tr>
<?php
while($row = mysql_fetch_array($result))

{
    $id = $row ['id'];
    $firstname = $row ['fname'];
    $lastname = $row ['lname'];
    $email = $row ['email'];
    $dob = $row ['dob'];
    $age = $row ['age'];
    $city = $row ['city'];
    $state = $row ['state'];
    $zip = $row ['zip'];
    $offence = $row ['offence'];
    $nots = $row ['notes'];

    echo '<tr>';
    echo '<td>'.$id.'</td>';
    echo '<td>'.$firstname.'</td>';
    echo '<td>'.$lastname.'</td>';
    echo '<td>'.$email.'</td>';
    echo '<td>'.$dob.'</td>';
    echo '<td>'.$age.'</td>';
    echo '<td>'.$city.'</td>';
    echo '<td>'.$state.'</td>';
    echo '<td>'.$zip.'</td>';
    echo '<td>'.$offence.'</td>';
    echo '<td>'.$notes.'</td>';
    echo '</tr>';
}
?>
</table></center>

<?php
mysql_close();
?>

答案 4 :(得分:0)

更正代码:

<html>
<head>
<title>Profile Database </title>
</head>
<body>
<?php
$connection = "connect.php";
require $connection;

mysql_select_db("bank");

$sql = "SELECT * FROM profiles";

$result = mysql_query($sql);
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>

<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
</thead>
<tbody>

 <?php

 while( $row = mysql_fetch_assoc( $result ))
 {
 ?>
 <tr>
 <td><?php echo $row['id'];?></td>
 <td><?php echo $row['fname'];?></td>
 <td><?php echo $row['lname'];?></td>
 <td><?php echo $row['email'];?></td>
 <td><?php echo $row['dob'];?></td>
 <td><?php echo $row['age'];?></td>
  <td><?php echo $row['city'];?></td>
 <td><?php echo $row['state'];?></td>
 <td><?php echo $row['zip'];?></td>
 <td><?php echo $row['offence'];?></td>
 <td><?php echo $row['notes'];?></td>
 </tr>
 <?php
 }
 ?>
 </tbody>
 </table>

 <?php 
 mysql_close($connection); 
 ?>

 </body>
 </html>