2011-11-09 34 views
0

我在我的數據庫中有一個訂單表,一旦用戶登錄到他的賬戶,他/她可以查看他/她以前的訂單。mysql語句顯示錶或消息

我有它下面的代碼:

<?php 

$result = mysql_query("SELECT * FROM `order` WHERE username = '". $_SESSION['username']."' ") 
or die(mysql_error()); ; 

echo "<table border='0'><table border width=100%><tr><th>Product</th><th>Quantity</th><th>Price</th><th>Date</th>"; 
while($info = mysql_fetch_array($result)) 
{ 
     echo "<tr>"; 
     echo "<td>" . $info['name']. "</td>"; 
     echo "<td>" . $info['quantity']. "</td>"; 
     echo "<td>" . $info['price']. "</td>"; 
     echo "<td>" . $info['date']. "</td>"; 

} 
echo "</tr>"; 
echo "</table>"; 
?> 

什麼mysql的語句來完成我用它來顯示信息「您還沒有訂購任何東西」,而不是一個空白表?

感謝

回答

0

您可以使用mysql_num_rows()獲取結果集中的行數。

$rows = mysql_num_rows($result); 
if($rows == 0) { 
    echo 'you have not ordered anything yet'; 
} 
0

你可以指望在你的聲明結果的數量使用mysql_num_rows()

if (mysql_num_rows($result) == 0) { 
    echo '<p>You have not ordered anything yet.</p>'; 
} 
else { 
    echo "<table..."; 
} 

而且,你可能要移動的倒數第二行(echo "</tr>";)的while循環內使HTML輸出正確。

1

使用mysql_num_rows

<?php 

$result = mysql_query("SELECT * FROM `order` WHERE username = '". $_SESSION['username']."' ") 
or die(mysql_error()); ; 

    if (mysql_num_rows($result) == 0) { 
     echo 'you have not ordered anything yet'; 
    } else { 
    echo "<table border='0'><table border width=100%><tr><th>Product</th><th>Quantity</th><th>Price</th><th>Date</th>"; 
    while($info = mysql_fetch_array($result)) 
    { 
      echo "<tr>"; 
      echo "<td>" . $info['name']. "</td>"; 
      echo "<td>" . $info['quantity']. "</td>"; 
      echo "<td>" . $info['price']. "</td>"; 
      echo "<td>" . $info['date']. "</td>"; 

    } 
    echo "</tr>"; 
    echo "</table>"; 
    }