Jump to content

while loop mysql_fetch_array single output


undertaker333

Recommended Posts

why it giving me single row data?? what i m missing here ... any help wil b appreciated..

 

<?php

$comments= "";

 

$result = mysql_query("SELECT * FROM comment");

while($row = mysql_fetch_array($result))

  { $serials= $row['serial'];

$names= $row['name'];

$emails= $row['email'];

$msges= $row['msg'];

$date_added= $row['date'];

$comments= "$names <br /> $msges <br /> $date_added";

}

 

?>

Link to comment
Share on other sites

Use freaking  CODE or PHP tags to post your code!

$comments= "";$result = mysql_query("SELECT * FROM comment");
while($row = mysql_fetch_array($result))
{
    $serials= $row['serial'];
    $names= $row['name'];
    $emails= $row['email'];
    $msges= $row['msg'];
    $date_added= $row['date'];
    $comments= "$names <br /> $msges <br /> $date_added";
}

 

Well, you are looping through the results of your query and assigning the values to variables. But, you are not using those variables in the loop, so on each subsequent iteration of the loop it is overwriting the values using the current record. So, you must have some code after that loop that is echoing the $comment. But, since you are doing that AFTER the loop it is only showing you the last value.

Link to comment
Share on other sites

Sir i m not that good in coding, could you please let me know how do that?

 

Well, you didn't show the code that outputs the data, so I don't know what to put inside the loop. You simply need to move the code that outputs the data inside the loop.

Link to comment
Share on other sites

it giving below output which is single row data, i want all rows data

 

Undertaker

tik sho o ka nah??? SHABA TAKRA SHA WARTHA :)

2011-08-25 18:09:03

 

You simply need to move the code that outputs the data inside the loop.

the code which output data for me is below and its inside the loop but loop executing it only once..

 $comments= "$names <br /> $msges <br /> $date_added";

 

 

in html body i just write this code

<?php echo $comments; ?>

 

may b it helps u to dig out where the problem lies. thank you for you time

Link to comment
Share on other sites

the code which output data for me is below and its inside the loop but loop executing it only once..

 $comments= "$names <br /> $msges <br /> $date_added";

No. That line does not output anything. It simply defined a variable called $comments. So, on the first iteration of the loop it sets $contents equal to the variables from the first record. On the second iteration it OVERWRITES the contents fo the first record with the content of the second records. So, when the loop completes it holds the contents of only one record - the last one!

 

So, if you are only using the variables in $comments, why do you have those other variables defined in the loop (e.g. $serials) that are not used?

 

Anyway, you can "concatenate" the output for each record onto the results of the previous record using ".=" when defining the variable. That means to add the text to the end of the current value.

$query = "SELECT name, msg, date FROM comment";
$result = mysql_query($query);

$comments= '';
while($row = mysql_fetch_assoc($result))
{
    $comments .= "{$row['name']}<br />{$row['msg']}<br />{$row['date']}<br /><br />\n";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.