?
Solved

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

Posted on 2005-04-11
11
Medium Priority
?
428 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:sam85281
  • 5
  • 4
10 Comments
 
LVL 32

Accepted Solution

by:
ldbkutty earned 2000 total points
ID: 13758849
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
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13758863
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
 
LVL 16

Author Comment

by:sam85281
ID: 13758992
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 16

Author Comment

by:sam85281
ID: 13758998
I'll give you the points for giving it a shot anyway.

-Sam
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13759019
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
 
LVL 6

Expert Comment

by:peyox
ID: 13759296
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
 
LVL 16

Author Comment

by:sam85281
ID: 13759790
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
 
LVL 16

Author Comment

by:sam85281
ID: 13759860
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
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13764651
Thanks for the response Sam. No need to apologize, it happens. :=)
0
 
LVL 16

Author Comment

by:sam85281
ID: 13766157
Glad it got changed for you.

-Sam
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question