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

时间:2015-12-10 07:42:14

标签: php html 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">
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 个答案:

答案 0 :(得分:0)

请使用<?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>

答案 1 :(得分:0)

你被关闭?>并打开<?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';
?>

答案 2 :(得分:0)

这是因为您混合to_representation()PHP不正确。在继续使用HTML元素之前,应首先附上PHP。

HTML

答案 3 :(得分:0)

您未正确关闭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>   

答案 4 :(得分:0)

你应该为php try <?php而不是<?

编写正确的语法
<?php echo $rows['firstname']; ?>

答案 5 :(得分:0)

你可以通过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.

?>