Problem with php code using mysqli for a search function: Table 'app.search' doesn't exist

Sorry if the answer for my problem exists in another location I had a search and was unable to find it.

I have recently been learning how to design webpages and decided that I wanted to put a search bar on a website and thus followed a tutorial on youtube. I followed it exactly as the producers produced the video and with that everything worked. However, as I will need to be able to put a search function into other websites I also created another database with another table using phpmadmin through the use of WAMP.

Database is named ‘app’ and the table is named ‘people’

The search function is very simple and consists of two php files which are named index-search.php and search.php

Copies of the file contents are - * note I took the second > from tags as it did not show in preview so you can see the code. They do exist in my php script.

index-search.php

html

  <head
    <title Search-Home </title
  </head
  
<body
  <form action="./search.php" method="get"    
        <input type="text" name="q" dir="ltr"
        <input type="submit" value="go"
  
  
  </form
  

</html

search.php

<?php
  $conn = mysqli_connect("localhost", "root", "", "app");
  
  if(mysqli_connect_errno())
  {
    echo "Failed to connect: " . mysqli_connect_error();
  }  
  
  
  
?>


<?php

//error_reporting(0);

  $output = ' ';

  if(isset($_GET ['q']) && $_GET['q'] !== ' ') 
    { 
       $searchq = $_GET['q']; 
        
        $q = mysqli_query($conn, "SELECT * FROM search WHERE keywords LIKE '%$searchq%' OR first_name LIKE '%$searchq%  '") or die (mysqli_error($conn));   
        $c = mysqli_num_rows($q);
        if($c == 0)
          {
            $output = 'No search results for <b>"' . $searchq . '"</b>'; 
            echo  'NO data found';
          }  
          
          else 
          {
              while($row = mysqli_fetch_array($q))
              {
                $id = $row['id']; 
                $first_name = $row['first_name'];
                $last_name = $row['last_name'];
                $bio = $row['bio'];
                $created = $row['created'];   
                
                $output .= '<a href=" ' . $id . '">
                  <h3> ' . $first_name . '</h3>
                  <p> ' . $last_name . '</p>
                  <p> ' . $bio . '</p>
                  <p> ' . $created . '</p>
                  </a>';
                
              }
          } 
          
         
    }   
     else
          {
            header("location: ./"); 
          }
    print("$output");
    mysqli_close($conn);
     

?>

I believe the problem is in relation to the search.php file this is because as noted above when I directly followed the tutorial including the databases shown named ‘tutorial’ with a table named ‘search’ it works. With this one I renamed the search.php to searchs.php and when I change the line in the 'index-search.php to the search query works but it connects to the tutorial database.

Not sure what I am doing wrong.

  1. The database and table exit.
  2. The file search.php exists.

If anyone can give me any ideas it would be really helpful.

The error quoted in your title “Table app.search” does not exist.

Try changing the table name from “search” to “people”

// EXISTING
$sql = '
  SELECT * 
  FROM search 
  WHERE keywords 
  LIKE "%$searchq%" 
  OR   first_name LIKE  "%$searchq%"  ';

// REVISED
$sql = '
  SELECT * 
  FROM people 
  WHERE keywords 
  LIKE "%$searchq%" 
  OR   first_name LIKE  "%$searchq%" ';

$q = mysqli_query ( $conn, $sql )  or die (mysqli_error($conn));   

Edit:
I am curious to know the YouTube video link.

This seems similar to a Ajax Live Search Database

John yes that was the problem I was so focused on the " " line in the index-search.php file and the fact it worked when I changed it to searchs that I did not see that. Very obvious once you point it out.

1 Like

Thanks SpacePhoenix yes that did the trick. As to the video the link is https://www.youtube.com/watch?v=xs9V8cdpP8I

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.