這是我的php代碼。如何從數據庫檢索信息並在表格中顯示?
<?php
session_start();
foreach($_POST AS $key => $val) {
$_SESSION[$key]=$val;
}
mysql_connect('localhost', 'bikec_user', '[email protected]');
mysql_select_db('bikecats_database');
$cnetid=$_POST['cnetid'];
$cpassword=$_POST['cpassword'];
$cnetid = stripslashes($cnetid);
$cpassword = stripslashes($cpassword);
$cnetid = mysql_real_escape_string($cnetid);
$cpassword = mysql_real_escape_string($cpassword);
$sql="SELECT RentalID, BikeID, RentalStartDate, RentalEndDate
FROM rental
WHERE CustTxStateNetID = '$cnetid'";
$result=mysql_query($sql) OR die(mysql_error());
$row=mysql_fetch_assoc($result);
$rentalid=$row['RentalID'];
$bikeid=$row['BikeID'];
?>
這是html代碼。它應該是顯示從數據庫中檢索到的查詢,但由於某種原因,當我運行它時,表格變爲空白。我知道我只是迴應兩個變數,但即使這些變數是空的。
<div class="span9">
<h2>My Account</h2>
<p><strong>My Rentals</strong></p>
<table class="table table-striped">
<thead>
<tr>
<th>Rental ID</th>
<th>Bike ID
<th>Check-Out Date</th>
<th>Return Date</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $rentalid ?></td>
<td><?php echo $bikeid ?></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table><br>
</div><!-- end span -->
使用[tag:PDO]進行連接。 'mysql_ *'已被棄用。 – hjpotter92 2013-03-21 01:02:41
嘗試var_dump($ row)查看它返回的內容..也使用PDO – 2013-03-21 01:03:19
您需要確認sql是否首先返回結果。 – Jesse 2013-03-27 23:20:14