簡體   English   中英

基本的PHP SQL查詢無法正常工作

[英]Basic PHP SQL Query not working

我們有一個基本的PHP腳本,可以從MySQL數據庫中提取每個作業的標題和描述,只需顯示這些信息即可。 看起來是這樣的:

$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>';
} ?>

該語句也不起作用。 這只是(奇怪地)顯示表第一行中每列的第一個字符。

有誰知道為什么它不能正常工作嗎?

在您使用的while使用與開始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);

您使用的結果變量是result不是results

更換

$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>';
} ?>

在循環開始之前,您已經獲取了第一行,這就是為什么它僅打印第二行的原因。 只需注釋掉這一行:

#$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

檢查一下:

$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>';
}
 ?>

更改此行

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM