Page 1 of 1

:: print all rows based on a selected field?

Posted: Fri Aug 24, 2007 10:11 pm
by synthetic
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I would like to print all entries in rows based on a certain field.

I have successfully integrated the following code (from php manual):

Code: Select all

<?php
$query = "SELECT * FROM table_name WHERE member = 'Juan'";
$result = mysql_query($query) or die(mysql_error());
echo '<pre>';
while($row = mysql_fetch_array($result)) {
print_r($row);
}
echo '</pre>';

?>
However, my printed results are weird. There are 2 mySQL entries for Juan...the query prints the following:
Array
(
[0] => Juan
[member] => Juan
[1] => 04/20/80
[date] => 04/20/80
[2] => Title1
[title] => Title1
[3] => This is my report description.
[description] => This is my report description.
[4] => 2007-08-24 23:09:52
[timestamp] => 2007-08-24 23:09:52
)
Array
(
[0] => Juan
[member] => Juan
[1] => 04/21/80
[date] => 04/21/80
[2] => Title2
[title] => Title2
[3] => This is my report description.
[description] => This is my report description.
[4] => 2007-08-24 23:09:36
[timestamp] => 2007-08-24 23:09:36
)
How can I clean this up?

desired printed result:
Juan
04/20/80
Title1
This is my report description.

Juan
04/21/80
Title2
This is my report description.

Please advise,

thanks


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Aug 24, 2007 10:30 pm
by s.dot
There's nothing weird about your print results, that I can see.

Posted: Fri Aug 24, 2007 10:39 pm
by feyd
Read the documentation concerning mysql_fetch_array()'s behavior.

Posted: Fri Aug 24, 2007 10:49 pm
by synthetic
thanks, will read


will post properly next time

Posted: Fri Aug 24, 2007 10:50 pm
by synthetic
scottaya wrote:There's nothing weird about your print results, that I can see
for the snippet, no...bad choice of phrasing on my part...

Posted: Fri Aug 24, 2007 11:16 pm
by synthetic
I got it! :D

Code: Select all

<?php

$result = mysql_query("SELECT * FROM internal_reports WHERE member = 'Juan'");

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
    printf ("<b> %s </b>  <br><br> %s <br> %s <br> %s <br><br>", $row["member"], $row["date"], $row["title"], $row["description"]);
}



mysql_free_result($result);

?>
prints this:
Juan

04/20/80
Title1
This is my report description.

Juan

04/21/80
Title2
This is my report description.

Juan

04/22/1980
Title3
Description
So, how can I print Juan only once (at the top)?


thanks!

Posted: Sat Aug 25, 2007 5:57 am
by feyd
Have a read through the first thread linked from Useful Posts (link in signature currently.)