我们有一个基本的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>';
} ?>
这种说法也不管用。这只是(奇怪地)显示表格第一行中每列的第一个字符。
有没有人有任何想法,为什么这不工作,因为它应该?
您使用
头台。非常感谢,不知道我是如何错过的。 $结果只是在实验中...只能踢我自己没有马上删除它,因为那时我会发现它为我自己。 –