简体   繁体   English

基本的PHP SQL查询无法正常工作

[英]Basic PHP SQL Query not working

We have a basic PHP script to extract the title and description for each job from a MySQL database as simply display this information. 我们有一个基本的PHP脚本,可以从MySQL数据库中提取每个作业的标题和描述,只需显示这些信息即可。 This is what it looks like: 看起来是这样的:

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

Now, this only extracts one row from the database, but it should extract two. 现在,这仅从数据库中提取一行,但应提取两行。 So, I tried the following to replace the while statement: 因此,我尝试使用以下命令替换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>';
} ?>

This statement doesn't work either. 该语句也不起作用。 This just displays (weirdly) the first character of each column in the first row in the table. 这只是(奇怪地)显示表第一行中每列的第一个字符。

Does anyone have any idea as to why this isn't working as it should? 有谁知道为什么它不能正常工作吗?

In your while use same variable $result as you started: 在您使用的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>';
}

and remove the first $results = mysql_fetch_assoc($query); 并删除第一个$results = mysql_fetch_assoc($query);

Result variable you have used is result not results 您使用的结果变量是result不是results

Replace 更换

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

to

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

You already fetched the first row before your loop started, which is why it only prints the second row. 在循环开始之前,您已经获取了第一行,这就是为什么它仅打印第二行的原因。 Simply comment out that line: 只需注释掉这一行:

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

You are also looping over $result but using $results in your while loop body. 您还循环$result但在while循环主体中使用$results

Check this: 检查一下:

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

Change this line 更改此行

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM