Avatar of edavo
edavoFlag for United States of America

asked on 

MySQL/Php query only returning one record?

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...
PHP

Avatar of undefined
Last Comment
jhp333
Avatar of sshah254
sshah254

Try

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

Ss
ASKER CERTIFIED SOLUTION
Avatar of jhp333
jhp333
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of edavo
edavo
Flag of United States of America image

ASKER

$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?
Avatar of edavo
edavo
Flag of United States of America image

ASKER

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

is working!!!!!!!!!
Avatar of mrh14852
mrh14852

$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

Avatar of edavo
edavo
Flag of United States of America image

ASKER

Why does it need the '%" in front of and behind the var?
Avatar of edavo
edavo
Flag of United States of America image

ASKER

Fast response = happy customer...
Avatar of Pratima
Pratima
Flag of India image

% 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

Avatar of mrh14852
mrh14852

% 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.

Avatar of edavo
edavo
Flag of United States of America image

ASKER

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";
Avatar of edavo
edavo
Flag of United States of America image

ASKER

Sorry I will post this as a NEW question so there is credit distributed...
Avatar of jhp333
jhp333
Flag of United States of America image

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

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo