-1

我正在尝试从我的 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>
4

5 回答 5

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>
于 2013-09-13T05:05:32.330 回答
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>
于 2013-09-13T07:07:42.870 回答
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();
?>
于 2013-09-13T05:11:15.133 回答
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>
于 2013-09-13T05:00:48.607 回答
0
WHILE($row = mysql_fetch_array($result))

应该是

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

或者

WHILE($row = mysql_fetch_assoc($result))

此外 mysql_* 函数已被弃用,强烈建议不要依赖它们。

请检查以下讨论:-

已弃用的 MySql 函数
如何使用已弃用的 mysql_* 函数成功重写旧的 mysql-php 代码?
http://php.net/manual/en/migration55.deprecated.php

使用MySQLiPDO

于 2013-09-13T05:03:17.773 回答