簡體   English   中英

如何從數據庫中選擇數據並將其顯示在表中?

[英]How to select a data from database and display it in a table?

我正在嘗試從數據庫中選擇數據,然后將其顯示在表中。 但是我不知道查詢或代碼出了什么問題。 幫我解決。

<?php

$host='localhost'; 
 $username='';
$password='';
 $database='reference';

mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($database)or die("cannot select DB");
$sql="SELECT * FROM TestTable";
$result=mysql_query($sql);
<table width="400" border="1" cellspacing="0" cellpadding="3">
while($rows=mysql_fetch_array($result)){
<tr>
<td width="30%"><? echo $rows['firstname']; ?></td>
<td width="30%"><? echo $rows['lastname']; ?></td>
<td width="30%"><? echo $rows['gender']; ?></td>
<td width="30%"><? echo $rows['console']; ?></td>
</tr>
}
</table>
?>
<?php
mysql_close();
?>
<?php
require_once 'Connection.php';
?>

請使用<?php標記而不是<?

<td width="30%"><?php echo $rows['firstname']; ?></td>
<td width="30%"><?php echo $rows['lastname']; ?></td>
<td width="30%"><?php echo $rows['gender']; ?></td>
<td width="30%"><?php echo $rows['console']; ?></td>

您對關閉?>和打開<?php

[ 注:不推薦使用mysql_*擴展名。 使用mysqli_*PDO ]

編輯代碼:

<?php

$host='localhost'; 
$username='';
$password='';
$database='reference';

$con = mysqli_connect($host,$username,$password,$database);
?>

<table width="400" border="1" cellspacing="0" cellpadding="3">
    <?php
    $result = mysqli_query($con,"SELECT * FROM TestTable");
    while($rows = mysqli_fetch_array($result,MYSQLI_ASSOC)) 
    {?>
        <tr>
            <td width="30%"><? echo $rows['firstname']; ?></td>
            <td width="30%"><? echo $rows['lastname']; ?></td>
            <td width="30%"><? echo $rows['gender']; ?></td>
            <td width="30%"><? echo $rows['console']; ?></td>
        </tr>
    <?php }?>
</table>

<?php
mysqli_close($con);
require_once 'Connection.php';
?>

這是因為您錯誤地混合了PHPHTML 在繼續處理HTML元素之前,您應該先封裝PHP。

<?php

    $host='localhost'; 
    $username='';
    $password='';
    $database='reference';

    mysql_connect($host, $username, $password)or die("cannot connect");
    mysql_select_db($database)or die("cannot select DB");
    $sql="SELECT * FROM TestTable";
    $result=mysql_query($sql);
?>
    <table width="400" border="1" cellspacing="0" cellpadding="3">
<?php
    while($rows=mysql_fetch_array($result)){
      echo '
        <tr>
          <td width="30%">'.$rows['firstname'].'</td>
          <td width="30%">'.$rows['lastname'].'</td>
          <td width="30%">'.$rows['gender'].'</td>
          <td width="30%">'.$rows['console'].'</td>
        </tr>';
    }
?>
    </table>

<?php
  mysql_close();
  require_once 'Connection.php';
?>

您沒有正確關閉PHP標記。 而且,除非您回顯它,否則您不能將HTML標記與PHP代碼混合使用。 考慮一下:

<?php

$host='localhost'; 
$username='';
$password='';
$database='reference';

mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($database)or die("cannot select DB");

$sql="SELECT * FROM TestTable";
$result=mysql_query($sql); ?> //<-- close here

<table width="400" border="1" cellspacing="0" cellpadding="3">
 <?php // <-- open here
 while($rows=mysql_fetch_array($result)){ ?> //<-- close here
  <tr>
    <td width="30%"><?php echo $rows['firstname']; ?></td>
    <td width="30%"><?php echo $rows['lastname']; ?></td>
    <td width="30%"><?php echo $rows['gender']; ?></td>
    <td width="30%"><?php echo $rows['console']; ?></td>
  </tr>
 <?php //<-- open here
 } ?>
</table>   
<?php
mysql_close();   
//require_once 'Connection.php'; already connect using above connection
?>

還有一兩件事, mysql_*擴展已被棄用,你應該使用mysqli_*PDO來代替。

Mysqli版本

<?php 
// database connection
$con = mysqli_connect("localhost","","");
mysqli_select_db($con, "reference");

$get_data = "select * from `TestTable`";
$run_data = mysqli_query($con, $get_data); 

?>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<?php // <-- open here
while ($rows=mysqli_fetch_array($run_data)) { ?>
 <tr>
    <td width="30%"><?php echo $rows['firstname']; ?></td>
    <td width="30%"><?php echo $rows['lastname']; ?></td>
    <td width="30%"><?php echo $rows['gender']; ?></td>
    <td width="30%"><?php echo $rows['console']; ?></td>
  </tr>
<?php //<-- open here
 } ?>
</table>   
<?php    
mysqli_close();
?>

PDO版本

<?php 
// database connection
$con = new PDO('mysql:host=localhost;dbname=reference;charset=utf8', '', '');
$get_data = "select * from `TestTable`";
$run_data = $con->query($get_data);

?>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<?php // <-- open here
while ($rows = $run_data->fetch(PDO::FETCH_ASSOC)) { ?>
 <tr>
    <td width="30%"><?php echo $rows['firstname']; ?></td>
    <td width="30%"><?php echo $rows['lastname']; ?></td>
    <td width="30%"><?php echo $rows['gender']; ?></td>
    <td width="30%"><?php echo $rows['console']; ?></td>
  </tr>
<?php //<-- open here
 } ?>
</table>   

您應該為php編寫正確的語法試試<?php而不是<?

<?php echo $rows['firstname']; ?>

您可以通過mysql和mysqli來完成。 但是由於不建議使用mysql,因此您應該使用mysqli。 但是仍然您的代碼在mysql中,所以我僅使用mysql解決了您的問題

  <?php

    $host='localhost'; 
    $username='';
    $password='';
    $database='reference';

    mysql_connect($host, $username, $password)or die("cannot connect");
    mysql_select_db($database)or die("cannot select DB");

    $sql="SELECT * FROM TestTable";
    $result=mysql_query($sql);
    ?>
    <table width="400" border="1" cellspacing="0" cellpadding="3">
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td width="30%"><? echo $rows['firstname']; ?></td>
    <td width="30%"><? echo $rows['lastname']; ?></td>
    <td width="30%"><? echo $rows['gender']; ?></td>
    <td width="30%"><? echo $rows['console']; ?></td>
    </tr>
    <?php
    }
    ?>
    </table>

    <?php
    mysql_close();
    //require_once 'Connection.php'; // I have commented this line coz I think you've already done connection at the top of the code.

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM