Forum Moderators: open
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
$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;?>