Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7960735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:50:23+00:00 2026-06-04T04:50:23+00:00

The query below will be used a search script. For some reason it won’t

  • 0

The query below will be used a search script. For some reason it won’t return all results where either condition is true. What am i doing wrong?

$sql = "SELECT name, id_code from codes WHERE name LIKE '%$q%' OR id_code 
LIKE '%$q%'";

$result = mysql_query($sql);

$query = mysql_query($sql) or die ("Error: ".mysql_error());

$num_rows1 = mysql_num_rows($result);

if ($result == "")
{
echo "";
}
echo "";


$rows = mysql_num_rows($result);

if($rows == 0)
{
print("<div id=norequests>No results for <strong>$q</strong></div>");

}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{

$name = htmlspecialchars($row['name']);
$code = htmlspecialchars($row['id_code']);

}

print("$code: $name<br /> <br />");
}

}


else{
    echo '<div id="error">No results for $q.</div>';
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:50:24+00:00Added an answer on June 4, 2026 at 4:50 am

    You are printing outside of while. Which means, no matter how many results you have, only the one will be printed.

    Either print inside the loop

    while($row = mysql_fetch_array($query))
    {
        $name = htmlspecialchars($row['name']);
        $code = htmlspecialchars($row['id_code']);
        print("$code: $name<br /> <br />");
    }
    

    or collect the variables in an array while looping and use them after the loop as you like

    $result_array = array();
    while($row = mysql_fetch_array($query))
    {
        $name = htmlspecialchars($row['name']);
        $code = htmlspecialchars($row['id_code']);
    
        $result_array[] = array(
            'name' => $name,
            'code' => $code
        );
    }
    print_r($result_array);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The below query i used to search the content of the MOSS 2007 site
I have some problem regarding the search in mysql. Below is my query. SELECT
Please help me figure a single query that will transform the data below... |id
I have the query below (shorted up all the fields in the select to
Why the two query below return duplicate member_id and not the third? I need
I've got a query below. I want to add a condition that if the
Below is my code which is used to upload images and then create some
I just found some sql query build like this in my project: return (new
I have tried to rewrite the below query, so that it will work in
I have a php page query mysql database, it will return about 20000 rows.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.