views:

59

answers:

5

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?

Thanks in advance, if you need any clarification, please ask!

Trufa

+2  A: 

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

Ignacio Vazquez-Abrams
+1  A: 

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

stew
A: 

Doesn't mysql_free_result clear the result set?

You have nothing left to loop over!

Johan
+1  A: 

You are freeing result after the first loop. Here

mysql_free_result($result);
 }

This should be

  }
mysql_free_result($result);
egis
Worked like a charme thanks!!
Trufa
+5  A: 

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

Slokun
Yep! exactly, thanks!
Trufa