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 8773075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:12:06+00:00 2026-06-13T18:12:06+00:00

I am creating a search function that will allow a user to search for

  • 0

I am creating a search function that will allow a user to search for a house in my database by postcode initially. The function can be seen below, when the function is executed and finds a true statement I get no errors however when I execute the search and I get a no fields been returned I am left with this error:

   No Records Found
   Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/undergradpad/search.php on line 26 

I want it to display No Records Found however I don’t know how I should correct the above error.

search.php:

<table width="500" border="1" cellpadding="5">
<tr>
<th width="16" scope="row">id</th>
<td width="95">bedrooms</td>
<td width="140">description</td>
<td width="104">roadname</td>
 <td width="71">postcode</td>
</tr>


<?php
    require("classes/class.House.inc");

    $obj = new House();
    $obj->search($_POST['term']);
    foreach($obj->data as $val){
    extract($val);
?>

<tr>
<td scope="row"><?php echo $id; ?></td>
<td><?php echo $bedrooms; ?></td>
<td><?php echo $description; ?></td>
<td><?php echo $roadname; ?></td>
<td><?php echo $postcode; ?></td>
</tr>
<?php
}
?>
</table>

classes/class.House.inc:

 <?php 
 include("connect/class.Database.inc");

 class House extends Database {


    public function read(){

            $query = "SELECT * FROM houses";

            $result = $this->mysqli->query($query);

            $num_result = $result->num_rows;    
            if($num_result > 0){
                while($rows =$result->fetch_assoc()){               
                    $this->data[]=$rows;
                    //print_r($rows);
                }           
                return $this->data;
            }
    }

    public function search ($term){

                    $query = "SELECT * FROM houses WHERE postcode like '%".$this->mysqli->real_escape_string($term)."%'";
                    $result = $this->mysqli->query($query);


                    $num_result = $result->num_rows;

                    if($num_result > 0){
                        while($rows =$result->fetch_assoc()){               
                            $this->data[]=$rows;
                            //print_r($rows);
                        }           
                        return $this->data;
                    } else{
                     echo 'No Records Found';    
                        }
    }
 }
 ?>
  • 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-13T18:12:08+00:00Added an answer on June 13, 2026 at 6:12 pm

    $obj is a House object. It has no $data property, even if you use it. The search method sets this property, but only if records are found. If no records are found, the method echoes a value.

    I would change it like this: Instead of echoing an error, make the method return false:

    public function search ($term){
    
        $query = "SELECT * FROM houses WHERE postcode like '%".$this->mysqli->real_escape_string($term)."%'";
        $result = $this->mysqli->query($query);
    
        $data = false;
    
        $num_result = $result->num_rows;
    
        while($row =$result->fetch_assoc()){               
            $data[]=$row;
        }           
        return $data;
    }
    

    Now, the function return an array of false if there is no data. You can now use it like this:

    $obj = new House();
    if ($data = $obj->search($_POST['term']))
    {
      foreach($obj->data as $val){
        extract($val);
      }
    }
    

    The changes I made:
    – No longer set data as a property, since you also return it. You can still do that, if you like, but I think it’s confusing to do both.
    – Return false if there’s no data.
    – Change the variable rows to row, since it only contains one row.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating an application that will search the documentation of a function within a
I am currently busy creating a search function that queries a user input string
Seems simple enough. I'm creating a search engine that returns results to the user
I'm creating eBay style search panes that allow users to narrow their results set
I'm creating a set of search panes that allow users to tweak their results
I'm creating a Java application where the user can search through a list of
Following on from a recent post. I am creating a search function that queries
I would like to find a Java library that will allow me to search
I am creating a search page where we can find the product by entering
I'm creating video music search in android WebView and I wanted users can launch

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.