2014-10-10 113 views
0

您好即時通訊新下訓練MySQL和PHP哪裏我錯了MySQL和PHP顯示錯誤

中AD01和AD03出現我的問題,請告訴我,他們是回聲刊登廣告,但AD02回聲正常

<?php require_once('Connections/localhost.php'); ?> 
<?php 
mysql_select_db($database_localhost, $localhost); 
$query_advtDisplay = "SELECT * FROM advt"; 
$advtDisplay = mysql_query($query_advtDisplay, $localhost) or die(mysql_error()); 
$row_advtDisplay = mysql_fetch_assoc($advtDisplay); 
$totalRows_advtDisplay = mysql_num_rows($advtDisplay); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Download Links</title> 
<link href="css/style.css" rel="stylesheet" type="text/css" /> 
</head> 
<body> 
<div class="wrapper"> 
    <div class="ad01"> 
     <?php if ($row_advtDisplay['advt-no']=='ad01') 
      { 
      echo $row_advtDisplay['advt-content']; 
      } 
      else{ echo "Advertise Here";} 
      ?> 
    </div> 
    <div class="middlebox"> 
     <div class="ad02"> 
      <?php if ($row_advtDisplay['advt-no']=='ad02') 
      { 
      echo $row_advtDisplay['advt-content']; 
      } 
      else{ echo "Advertise Here";} 
      ?> 
     </div> 
     <div class="linkbox"> 
      <p>Download Links</p> 
      <ul> 
       <li><a href="#">Link 01</a></li> 
       <li><a href="#">Link 02</a></li> 
       <li><a href="#">Link 03</a></li> 
       <li><a href="#">Link 04</a></li> 
       <li><a href="#">Link 05</a></li> 
      </ul> 
      <ul> 
       <li><a href="#">Link 06</a></li> 
       <li><a href="#">Link 07</a></li> 
       <li><a href="#">Link 08</a></li> 
       <li><a href="#">Link 09</a></li> 
       <li><a href="#">Link 10</a></li> 
      </ul> 
      <ul> 
       <li><a href="#" target="_blank">Link 11</a></li> 
       <li><a href="#" target="_blank">Link 12</a></li> 
       <li><a href="#" target="_blank">Link 13</a></li> 
       <li><a href="#" target="_blank">Link 14</a></li> 
       <li><a href="#" target="_blank">Link 15</a></li> 
      </ul> 
     </div> 
     <div class="passwordbox"> 
      <p>RAR Password</p> 
     </div> 
    </div> 
    <div class="ad03"><?php if ($row_advtDisplay['advt-no']=='ad03') 
      { 
      echo $row_advtDisplay['advt-content']; 
      } 
      else{ echo "Advertise Here";} 
      ?></div> 
    <div class="clear"></div> 
</div> 
</body> 
</html> 
<?php 
mysql_free_result($advtDisplay); 
?> 

只有正確AD02不顯示AD01和AD03 與表只編碼問題對不起,沒有問題的英語不好

+0

我不得不假設,那麼...... $ row_advtDisplay ['advt-no'] ='ad02''因此'ad01'和'ad03'命中'else'語句。 – 2014-10-10 23:15:15

+1

您只提取結果集中的第一行。請閱讀關於循環拋出一個結果集...在這裏例如:http://php.net/manual/en/mysqli-result.fetch-assoc.php – 2014-10-10 23:15:49

+0

等等...什麼是PHP錯誤? – 2014-10-10 23:32:47

回答

2

mysql_fetch_assoc只會返回一個排有史以來...所以你需要通過看環你所有的數據。

$just_one_row = mysql_fetch_assoc($advtDisplay); 

// how to loop through all the rows 
while ($row = mysql_fetch_assoc($advtDisplay)) { 
    echo $row["advt-no"]; 
    echo $row["advt-content"]; 
} 

// maybe do something like... 
$mydata = array(); 
while ($row = mysql_fetch_assoc($advtDisplay)) { 
    $mydata[ $row['advt-no'] ] = $row['advt-content']; 
} 
<!-- and then --> 
<div class="ad01"> 
    <?php 
    if (isset($mydata['ad01']) && !empty($mydata['ad01'])) { 
     echo $mydata['ad01'];  
    } else { echo "Advertise Here"; } ?> 
</div> 

,我不得不說,這...你真的應該使用PDOmysqli