0

I wrote this code:

<?php
mysql_connect("localhost", "root", "root") or
    die("Could not connect: " . mysql_error());
mysql_select_db("internal");

$result = mysql_query("SELECT Title, Message FROM msg");
?>

<?php

  while ($row = mysql_fetch_array($result, MYSQL_NUM)){
?>

     <div>
         <h3><a href="#"><?php printf("%s", $row[0]); ?></a></h3>
         <div><?php printf("%s", $row[1]); ?></div>
     </div>

<?php
 mysql_free_result($result);
  }
?>

The result I´m getting is the first row of the MySQL table (With the correct formatting) I include an Image just in case:

screeshot result

(This is in fact the first row of my MySQL DB and the only thing I see)

The code was looping until I had to add the html tags, what I mean is that if I just do:

<?php printf("%s", $row[0]); ?>
&
<?php printf("%s", $row[1]); ?>

It looped and brought all of the results.

Could this be a syntax error?

1
  • Thanks to all! The problem is definitely solved! Commented Oct 14, 2010 at 16:06

5 Answers 5

5

mysql_free_result($result); inside your while loop. This makes it so after the first iteration, it clears the results, making so no more can be grabbed.

Change your ending to

<?php
  }
mysql_free_result($result);
?>

and it'll fix it

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

Comments

2

Your third-to-last line frees the result before you're done with it. Move it outside the loop.

Comments

1

move the call to mysql_free_result to be outside of the while loop.

Comments

1

You are freeing result after the first loop. Here

mysql_free_result($result);
 }

This should be

  }
mysql_free_result($result);

Comments

0

Doesn't mysql_free_result clear the result set?

You have nothing left to loop over!

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.