3

I want to create a form which allows the user to type in a search and have it pick up the right values from a database and display them, for some reason I can't get my query to work it just displays "could not search"

Here is my php code

 <?php

  include "connect.php";

  $output = '';

  if(isset($_POST['search'])) {
    $search = $_POST['search'];
    $search = preg_replace("#[^0-9a-z]i#","", $search);

    $query = mysqli_query("SELECT * FROM house WHERE town LIKE '%$search%'") or die ("Could not search");
    $count = mysqli_num_rows($query);
    
    if($count == 0){
      $output = "There was no search results!";

    }else{

      while ($row = mysqli_fetch_array($query)) {

        $town = $row ['town'];
        $street = $row ['street'];
        $bedrooms = $row ['bedrooms'];
        $bathroom = $row ['bathrooms'];

        $output .='<div> '.$town.''.$street.''.$bedrooms.''.$bathrooms.'</div>';

      }

    }
  }

  ?>

Here is my form

 <form action ="home.php" method = "post">
  
          <input name="search" type="text" size="30" placeholder="Belfast"/>

          <input type="submit" value="Search"/>

          </form> 

          <?php print ("$output");?>

4
  • replace '%$search%' with '%".$search."%'... Commented Mar 30, 2015 at 13:20
  • didn't work, still says 'Could not search!' Commented Mar 30, 2015 at 13:22
  • echo your query SELECT * FROM house WHERE town LIKE '%$search%' and try the query from phpmyadmin or mysql... Commented Mar 30, 2015 at 13:24
  • This might help. askseeker.com/code-snippets/search-form-php-using-mysqli Commented Feb 13, 2018 at 8:34

3 Answers 3

2

You're not connecting to your DB in your query:

$query = mysqli_query("SELECT
                      ^ missing connection variable

there is no connection variable (unknown what you are using to connect with)

$query = mysqli_query($connection, "SELECT ...
                      ^^^^^^^^^^^^

From the manual http://php.net/manual/en/mysqli.query.php

Object oriented style mixed mysqli::query ( string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

Procedural style mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

figuring you are using mysqli_ to connect with. If you're using mysql_ or PDO to connect with, that won't work. Those different MySQL APIs do not intermix with each other.

Plus, instead of or die ("Could not search") do or die(mysqli_error($connection)) to catch any errors, if any.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.


Example mysqli connection:

$connection = mysqli_connect("myhost","myuser","mypassw","mybd") 
                or die("Error " . mysqli_error($connection)); 

For more information, visit:

Sign up to request clarification or add additional context in comments.

6 Comments

Can you please explain it for my knowledge? Error reporting should only be done in staging, and never production.
@Testing Should there be any errors that will display system paths, db tables/column names the person does not want to show, connection credentials, those should not be displayed to the user.
I have a connection to the database in a connect file which I already have included
@Rebekah You still need to pass the connection variable to your query. I don't know which variable you're using to connect with, which is why I used $connection as an example. You will have to replace it with the one you're using.
Just change your query to $query = mysqli_query($con, "SELECT * FROM house WHERE town LIKE '%$search%'") or die ("Could not search"); - Here I used $con, seeing that's what you're using as a variable in your other question. You need to pass the connection to your query, just reload my answer also. @Rebekah
|
0
$query = mysqli_query("SELECT * FROM house WHERE town LIKE '%$search%'") or die ("Could not search");

$result = mysqli_query($connection,$query);

$count = mysqli_num_rows($result);

The $connection is the variable declared in your connect.php to connect to your database .

You should have out the $result before the $count.

Comments

-1

Try to remove the space between if($count==0) it will start working!!

if($count==0){
      $output = "There was no search results!";}

1 Comment

Despite the fact that this might be solved or not of relevance after four years spaces in this context have to effect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.