duanjianxu4288 2015-12-10 07:42
浏览 63
已采纳

如何从数据库中选择数据并将其显示在表中?

I'm trying select the data from database and then display it in a table. But i don't know what's wrong with the query or the code. Help me to resolve it.

<?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';
?>
  • 写回答

6条回答 默认 最新

  • dongtiao6362 2015-12-10 07:48
    关注

    You're not closing properly PHP tag. And you can't mix HTML markup together with PHP code unless you echoing it. Consider this :

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

    And one more thing, mysql_* extension are deprecated and you should used mysqli_* or PDO instead.

    Mysqli version

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

    <?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>   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退