Jump to content

listing multiple results from a mysql query


ldb358

Recommended Posts

i want it to list the first 10 results

thanks in advanced

 

heres my code:

function runBrowse($search){
$qSearch = mysql_query("SELECT * FROM users WHERE username LIKE '%$search%'");
$results = mysql_fetch_array($qSearch);
if($results['username'] != ""){
echo $results['username'];
}else{
echo "no results";
}
}


function runBrowse($search){
$qSearch = mysql_query("SELECT * FROM users WHERE username LIKE '%$search%'" LIMIT 10);
while($results = mysql_fetch_array($qSearch)) {
if($results['username'] != ""){
echo $results['username'];
}else{
echo "no results";
}
}}

There's no reason to check if the username is blank when you have the LIKE clause that only selects the usernames with search in them.  If they are blank, they won't be selected.

 

Try:

 

function runBrowse($search){
   $qSearch = mysql_query("SELECT * FROM users WHERE username LIKE '%$search%' LIMIT 10");
   while($results = mysql_fetch_array($qSearch))
   {
      echo $results['username'];
   }
}

 

And please learn to properly, or at least consistently, indent and format.

btw, if you want to tell ur users that there are no results, change to this:


function runBrowse($search){
$qSearch = mysql_query("SELECT * FROM users WHERE username LIKE '%$search%' LIMIT 10");
$rows = mysql_num_rows($qSearch);
if ($rows == 0) {
echo "no results";}
else {	

while($results = mysql_fetch_array($qSearch)) {echo "no results";}
}}

Ted

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.