:: print all rows based on a selected field?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
synthetic
Forum Newbie
Posts: 4
Joined: Fri Aug 24, 2007 9:58 pm

:: print all rows based on a selected field?

Post 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]
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

There's nothing weird about your print results, that I can see.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Read the documentation concerning mysql_fetch_array()'s behavior.
synthetic
Forum Newbie
Posts: 4
Joined: Fri Aug 24, 2007 9:58 pm

Post by synthetic »

thanks, will read


will post properly next time
synthetic
Forum Newbie
Posts: 4
Joined: Fri Aug 24, 2007 9:58 pm

Post 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...
synthetic
Forum Newbie
Posts: 4
Joined: Fri Aug 24, 2007 9:58 pm

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Have a read through the first thread linked from Useful Posts (link in signature currently.)
Post Reply