2015-12-10 127 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

相關問題