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";
}
}


Link to comment
Share on other sites

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";
}
}}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.