0

We have a basic PHP script to extract the title and description for each job from a MySQL database as simply display this information. 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:

<?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?

5 Answers 5

3

In your while use same variable $result as you started:

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

Sign up to request clarification or add additional context in comments.

Comments

2

Result variable you have used is result not 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>';
} ?>

1 Comment

Head-desk. Thanks so much, don't know how I missed that. $results was only in there still from experimentation… Can only kick myself for not deleting it straight away as then I would have spotted it for myself.
1

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.

Comments

0

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

Comments

0

Change this line

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

to

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.