【问题标题】:Display database table in PHP array在 PHP 数组中显示数据库表
【发布时间】:2013-09-17 16:26:52
【问题描述】:

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

【问题讨论】:

  • 您没有打开/关闭表格行 - &lt;tr&gt;...&lt;/tr&gt;
  • 您是否遇到任何错误?它似乎不起作用,意味着您遇到错误或 o/p 不是您想要的..
  • 你缺少&lt;tr&gt;标签,mysql_函数也被弃用了,你不应该再使用它们,了解prepared statements,改用PDOMySQLi-@987654324 @ 将帮助您决定哪个。
  • 非常感谢,我不知道。哈哈,我在过去的半小时里一直在阅读它,这似乎是我需要了解更多的东西。

标签: php mysql database


【解决方案1】:

试一试:

<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>

【讨论】:

  • 我试过了,它确实显示了表名,但没有填充
【解决方案2】:
WHILE($row = mysql_fetch_array($result))

应该是

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

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

【讨论】:

  • 我设法让表格显示标题,但表格中的信息仍未填充。我们正在连接,它确实可以识别数据库和表,
  • mysql和mysqli有什么区别?
【解决方案3】:
<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>

【讨论】:

  • 非常感谢。这对我有帮助。这显示了表头和边界,但似乎没有填充数据。我正在连接,它确实看到了表格。
  • 欢迎..请接受作为答案点击我的答案左侧的勾号。
【解决方案4】:

试试这个代码:

<?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();
?>

【讨论】:

    【解决方案5】:

    更正的代码:

    <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>
    

    【讨论】:

      猜你喜欢
      • 2020-10-30
      • 2017-10-17
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      相关资源
      最近更新 更多