2012-10-09 94 views
0

我們有一個基本的PHP腳本,可以從MySQL數據庫中爲每個作業提取標題和說明,只需顯示此信息。這看起來是這樣的:基本PHP SQL查詢不起作用

$sql  = "SELECT `title`, `desc` FROM jobs WHERE active = 'y'"; 
$query  = mysql_query($sql) or die('<em><strong>SQL Error:</strong> ' . mysql_error() . '</em>'); 
$results = mysql_fetch_assoc($query); 

<?php while($result = mysql_fetch_assoc($query)) { 
    echo '<div class="left_content" style="margin-top: 15px;">'; 
    echo "<h2>{$results['title']}</h2>"; 
    echo "<p>{$results['desc']}</p>"; 
    echo '</div>'; 
} ?> 

現在,這隻從數據庫中提取一行,但它應該提取兩個。所以,我嘗試以下,以取代while聲明:

<?php foreach($results as $result) { 
    echo '<div class="left_content" style="margin-top: 15px;">'; 
    echo "<h2>{$result['title']}</h2>"; 
    echo "<p>{$result['desc']}</p>"; 
    echo '</div>'; 
} ?> 

這種說法也不管用。這只是(奇怪地)顯示錶格第一行中每列的第一個字符。

有沒有人有任何想法,爲什麼這不工作,因爲它應該?

您使用

回答

2

結果變量resultresults

更換

$sql  = "SELECT `title`, `desc` FROM jobs WHERE active = 'y'"; 
$query  = mysql_query($sql) or die('<em><strong>SQL Error:</strong> ' . mysql_error() . '</em>'); 
**$results = mysql_fetch_assoc($query);** // remove this line 

<?php while($result = mysql_fetch_assoc($query)) { 
    echo '<div class="left_content" style="margin-top: 15px;">'; 
    echo "<h2>{$results['title']}</h2>"; 
    echo "<p>{$results['desc']}</p>"; 
    echo '</div>'; 
} ?> 

$sql  = "SELECT `title`, `desc` FROM jobs WHERE active = 'y'"; 
$query  = mysql_query($sql) or die('<em><strong>SQL Error:</strong> ' . mysql_error() . '</em>'); 


<?php while($result = mysql_fetch_assoc($query)) { 
    echo '<div class="left_content" style="margin-top: 15px;">'; 
    echo "<h2>{$result['title']}</h2>"; 
    echo "<p>{$result['desc']}</p>"; 
    echo '</div>'; 
} ?> 
+0

頭臺。非常感謝,不知道我是如何錯過的。 $結果只是在實驗中...只能踢我自己沒有馬上刪除它,因爲那時我會發現它爲我自己。 –

3

在你while使用相同的變量$result你開始:

while($result = mysql_fetch_assoc($query)) { 
    echo '<div class="left_content" style="margin-top: 15px;">'; 
    echo "<h2>{$result['title']}</h2>"; 
    echo "<p>{$result['desc']}</p>"; 
    echo '</div>'; 
} 

並刪除第一個$results = mysql_fetch_assoc($query);

1

你已經獲取的第一行之前你的循環開始了,這就是爲什麼它只能打印在第二排。只是註釋掉該行:

#$results = mysql_fetch_assoc($query); # here is your first row, 
             # simply comment this line 

<?php while($result = mysql_fetch_assoc($query)) { 
    echo '<div class="left_content" style="margin-top: 15px;">'; 
    echo "<h2>{$result['title']}</h2>"; 
    echo "<p>{$result['desc']}</p>"; 
    echo '</div>'; 
} ?> 

您還遍歷$result但在while循環機身採用$results

0

檢查:

$sql  = "SELECT `title`, `desc` FROM jobs WHERE active = 'y'"; 
$query  = mysql_query($sql) or die('<em><strong>SQL Error:</strong> ' . mysql_error() . '</em>'); 


<?php 
while($result = mysql_fetch_assoc($query)) { 
    echo '<div class="left_content" style="margin-top: 15px;">'; 
    echo "<h2>{$result['title']}</h2>"; 
    echo "<p>{$result['desc']}</p>"; 
    echo '</div>'; 
} 
?> 
0

改變這一行

<?php while($result = mysql_fetch_assoc($query)) { 

<?php while($results = mysql_fetch_assoc($query)) {