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!

Recommended Answers

All 4 Replies

Member Avatar for diafol

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

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

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

Quick question, what does "PHP_EOL" do?

Member Avatar for diafol

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.