What's wrong with this php/mysql fetch???

This is driving me nuts, can't tell what I'm missing.

Here's my code:
<?
$db_name = "dbname";
$table_name = "news_table";
$connection = @mysql_connect("localhost","user","pass") or die(mysql_error());
$db = @mysql_select_db($db_name,$connection) or die(mysql_error());

$sql ="SELECT date, entry, stamp FROM $table_name ORDER BY stamp DESC";
$result =@mysql_query($sql,$connection)or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
      $date = $row['date'];
      $entry = $row['entry'];
      $news_list = "<font color=\"#ffffff\" face=\"Arial\" size=2><b>$date</b></font><br>";
      $news_list .= "<font color=\"#cccccc\" face=\"Arial\" size=2>";
      $news_list .= nl2br("$entry");
      $news_list .= "</font>";
      $news_list .= "<br><br>";
}
$news_list .= "<br>";
?>
<html>
<?php echo "$news_list"; ?>
blah blah blah

For some reason, it won't loop through all the rows.  It only displays the first?????

Anyone?!?!

Thanks,
-Sam
LVL 16
sam85281Asked:
Who is Participating?
 
ldbkuttyCommented:
change this line:

$news_list = "<font color=\"#ffffff\" face=\"Arial\" size=2><b>$date</b></font><br>";

to this:

$news_list .= "<font color=\"#ffffff\" face=\"Arial\" size=2><b>$date</b></font><br>";

// I added the appending symbol . (dot) and I hope you understand the problem!
0
 
ldbkuttyCommented:
well, if you can't predict the problem: Everytime the control comes within the loop, value of $news_list is overwritten. The line you have is "assignment" statement and I changed it to "appending" statement!
0
 
sam85281Author Commented:
Well you have to start your definition of a variable WITHOUT the dot before you can start adding to it with a dot.  If you start with a dot on the first one, php will error searching for the first part which it will never find.

Besides that, I forgot I had it working on another page, so copied it over and it works fine.  I've still looked through the code 100 times to see the difference and can't find it????

But...as long as it works.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
sam85281Author Commented:
I'll give you the points for giving it a shot anyway.

-Sam
0
 
ldbkuttyCommented:
With all due respect, I can't accept C Grade for this question.

>>  If you start with a dot on the first one, php will error searching for the first part which it will never find.

It depends on the error_reporting ( http://www.php.net/error_reporting ) you have in php.ini. With NOTICE error being suppresed (which is NOT insignificant), it is NOT a problem to have "append" statement before initialization!
0
 
peyoxCommented:
Ldbkutty IS RIGHT. He solved your problem and grade C is well not appropriate.

>> I'll give you the points for giving it a shot anyway
Your behaviour is not appropriate either.

Please read what C grade means:
http://www.experts-exchange.com/help.jsp#hi73

Answering your question (with proper initialization) for your future reference:

$news_list = "";
while ($row = mysql_fetch_array($result)) {
     $date = $row['date'];
     $entry = $row['entry'];
     $news_list .= "<font color=\"#ffffff\" face=\"Arial\" size=2><b>$date</b></font><br>";
     $news_list .= "<font color=\"#cccccc\" face=\"Arial\" size=2>";
     $news_list .= nl2br("$entry");
     $news_list .= "</font>";
     $news_list .= "<br><br>";
}

Good luck.
0
 
sam85281Author Commented:
There was no offense meant by "giving the points anyway".  Or by grading it a C.

I'll admit, I didn't give any thought to what the grade meant.  I simply fixed the problem before ldbkutty's reply, and that wasn't the problem.

I should've just closed the question, but since there was already a reply, I figured I'd award the points.

I'm a contributing expert myself in the Flash section, and would never intentionally do anything to offend another contributor here.

It wasn't meant that way, and I appologize if it was taken that way.

I've never paid much attention to the grades, and didn't see it as giving a "bad mark".

I'm not aware of any way to change a grade, but if I can find a way I will change it because my intention was not to give a bad grade.

-Sam

0
 
sam85281Author Commented:
I have put in a request to change the grade.

Like I said, I did not mean to leave a "bad grade" and didn't look at that way at the time.

Once again, I appologize and hopefully it will be fixed.

Thanks,
-Sam
0
 
ldbkuttyCommented:
Thanks for the response Sam. No need to apologize, it happens. :=)
0
 
sam85281Author Commented:
Glad it got changed for you.

-Sam
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.