Forum Moderators: open

Message Too Old, No Replies

Outputting if multiple records match

MySQL database

         

Jeremy_H

6:32 pm on Aug 8, 2006 (gmt 0)

10+ Year Member



Hello,

How would I go about pulling information from my database if multiple records match?

Lets say I have a database named TEAMS that has three columns,

TEAM, CITY, STATE

$query=mysql_query("SELECT team, city FROM teams WHERE state='CA'");
while($row=mysql_fetch_object($query)){$team=$row->team;$city=$row->city;}

If I have the following code:

<?php echo("<p>$team - $city, CA</p>");?>

All I get is the last instance in my database that matches.

How would I go about outputing all of my CA teams, like this:

<p>Red - Sacramento, CA</p>
<p>Green - Sacramento, CA</p>
<p>Blue - San Francisco, CA</p>

Thanks

volatilegx

3:14 pm on Aug 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to put the echo part inside the braces...

$query=mysql_query("SELECT team, city FROM teams WHERE state='CA'");
while($row=mysql_fetch_object($query)){
$team=$row->team;$city=$row->city;
echo("<p>$team - $city, CA</p>");
}

Or, if that is not doable, you could create a string and then echo the string, like this:

$team_str = '';
$query=mysql_query("SELECT team, city FROM teams WHERE state='CA'");
while($row=mysql_fetch_object($query)){
$team=$row->team;$city=$row->city;
$team_str .= "<p>$team - $city, CA</p>\n";
}

Then later on, you have this:

<?php echo $team_str;?>