0

MySQL データベースから各ジョブのタイトルと説明を抽出し、この情報を単純に表示するための基本的な PHP スクリプトがあります。これは次のようになります。

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

これで、データベースから 1 行しか抽出されませんが、2 行抽出されるはずです。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>';
} ?>

このステートメントも機能しません。これは、テーブルの最初の行の各列の最初の文字を (奇妙なことに) 表示するだけです。

なぜこれがうまくいかないのか、誰にも分かりませんか?

4

5 に答える 5

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);

于 2012-10-09T10:07:49.917 に答える
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>';
} ?>
于 2012-10-09T10:07:45.173 に答える
1

ループが開始する前に最初の行を既にフェッチしているため、2 行目のみが出力されます。その行をコメントアウトするだけです:

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

あなたもループしていますが、whileループ本体で$result使用しています。$results

于 2012-10-09T10:08:46.920 に答える
0

この行を変更します

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

<?php while($results = mysql_fetch_assoc($query)) {
于 2012-10-09T10:10:55.603 に答える
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>';
}
 ?>
于 2012-10-09T10:09:11.677 に答える