2012-10-09 92 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)) {