0

Hello all, being relatively new to PHP, I'm in search of some help regarding displaying data that is contained in my database. It's a very simple script that allows users to type in their alias and their comment on a website. Here's the code I'm using to display the data:

$result = mysql_query("SELECT * FROM comments");
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"alias");
$f2=mysql_result($result,$i,"comment");
$i++;
}
echo $f1;
echo " : ";
echo $f2;

But when the code executes, the only thing displayed is the colon ":".

I know that there is atleast 6 records in the database, so lack of data is not the issue.

Any suggestions?

Thanks!

3
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by diafol
0

1. Don't use mysql_result - use mysql_fetch_array() 2. THis $num=mysql_numrows($result); should be $num=mysql_num_rows($result);

Edited by diafol: n/a

1

try:

$result = mysql_query("SELECT * FROM comments") or die(mysql_error());
while( $row = mysql_fetch_assoc($result) )
{
  echo $row['alias'], ': ', $row['comment'], '<br />', PHP_EOL;
}
0

Thank you for your help guys, it works! :)

Quick question, what does "PHP_EOL" do?

0

It adds a newline in an OS browser-compatible way - instead of relying on "\n" - which may not display your nice ordered html.

It has no real effect on the code AFAIK, just the underlying format of the HTML - what you see under 'view source' of your browser.

If solved, mark it so with the link below.

Edited by diafol: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.