MySQL/Php query only returning one record?

edavo
edavo used Ask the Experts™
on
The code below is returning only ONE record. I want it to return ALL records that meet the query... It should return ALL records whose "team" field equals the value of $tm...

The $tm value is being passed through the url string and grabbed via a GET statement. I echoed the $tm and it is working properly.

------- code

$sql="SELECT * FROM schedules WHERE team LIKE '".$tm."' ORDER BY date";
            $result=mysql_query($sql) or die(mysql_error());
            $count=mysql_num_rows($result);
            
        while($rows=mysql_fetch_array($result)){
                  echo '<tr><td>';
              echo $rows['opp'];
              echo '</td><td>';
                  echo $rows['loc'];
                  echo '</td><td>';
                  echo $rows['date'];
                  echo '</td></tr>';
                  }

Please review and help...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Try

$sql="SELECT * FROM schedules WHERE team LIKE '*".$tm."*' ORDER BY date";

Ss
Commented:
I think sshah254 meant:

$sql="SELECT * FROM schedules WHERE team LIKE '%".$tm."%' ORDER BY date";

Author

Commented:
$sql="SELECT * FROM schedules WHERE team LIKE '*".$tm."*' ORDER BY date";

$sql="SELECT * FROM schedules WHERE team LIKE '%".$tm."%' ORDER BY date";

I tried both above - still only get one record?
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Author

Commented:
$sql="SELECT * FROM schedules WHERE team LIKE '%".$tm."%' ORDER BY date";

is working!!!!!!!!!

Commented:
$result = mysql_query("SELECT * FROM schedules WHERE team LIKE '%$tm%' ORDER BY date") or die(mysql_error());
$count = mysql_num_rows($result);
            
while ($rows = mysql_fetch_array($result))
{
	echo '<tr><td>';
	echo $rows['opp'];
        echo '</td><td>';
	echo $rows['loc'];
	echo '</td><td>';
	echo $rows['date'];
	echo '</td></tr>';
}

Open in new window

Author

Commented:
Why does it need the '%" in front of and behind the var?

Author

Commented:
Fast response = happy customer...
% means it will match all the values which having the word n that var

For example

if var value is 'ab'

then values will match like

ab

sssabfff

Commented:
% is a wildcard and is used with LIKE and typically meant for database searches.  LIKE can be slow on large databases so it's always best to use indexes and exact phrases where possible.

Author

Commented:
What if I want to add a second parameter? I understand the % but I want the sport var to be an exact match...

This works for one parameter:
$sql="SELECT * FROM schedules WHERE team LIKE '%".$tm."%' ORDER BY date";


This is not working for two:
$sql="SELECT * FROM schedules WHERE team LIKE '%".$tm."%' AND sport='%".$sport."%'ORDER BY date";

Author

Commented:
Sorry I will post this as a NEW question so there is credit distributed...

Commented:
% only works with LIKE operator, not with = operator.
Change "sport = ..." to "sport LIKE ..."

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial