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

问题描述:

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

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>   

Please use <?php tags instead of <?

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

You are messed with closing ?> and opening <?php.

[NOTE: mysql_* Extension Are Deprecated. Use mysqli_* or PDO ]

Edited Code :

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

It is because your mixing PHP and HTML incorrectly. You should enclose first your PHP before proceeding with an HTML elements.

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

you should write proper syntax for the php try <?php instead of <?

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

You can do it by both mysql and mysqli. But since mysql is deprecated now you should use mysqli. But still your code was in mysql so I solved your issue using mysql only

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

?>