2015-12-10 220 views
0

我想从数据库中选择数据,然后将其显示在表中。但我不知道查询或代码有什么问题。帮我解决它。如何从数据库中选择数据并将其显示在表格中?

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

你是否收到任何错误? –

+0

你有什么错误吗? – Sadikhasan

+0

是的。通过获取网站无法显示页面。 – ganeshvasanth

回答

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

不可以删除它。帮助一个完美的coad。 – ganeshvasanth

+0

在这里,你可以尝试'mysqli'和'PDO' –

+0

谢谢你的回应。我得到了输出。 – ganeshvasanth

0

请使用而不是<?

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

您与关闭?>开放<?php混乱<?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'; 
?> 
+1

感谢您的回复。我得到了输出。 – ganeshvasanth

+0

Ok @ganeshvasanth:我先回答,甚至你有输出。那么,为什么要解决我的问题。 **我的努力是徒劳的。** –

+1

@NanaPartykar:你帮助他只是为了赏金吗? –

0

这是因为你的混合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'; 
?> 
0

你应该写的PHP的正确语法尝试的<?php代替<?

<?php echo $rows['firstname']; ?> 
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. 

?> 
+0

@ ganeshvasanth是不是有用的答案或它有一些问题? – Nehal

相关问题