Php – How to display MYSQL data into PHP search box

MySQLPHP

Hello I am having massive problems with this task for my assignment

I have a database set up on xampp called search_test which has firstname and lastname as fields in it. I have set up a php form so when the user types in a name say Andre it returns all the andres in the database. There is a problem it keeps telling me there are no search results even though i know there is data in the database Here is the code supposed to be one php page called index.php

    <?php
    mysql_connect("localhost","michael","xcA123sd") or die(mysql_error());
    mysql_select_db("search_test") or die ("could not find db"); 
     $output ='';
    if (isset ($_POST['search']));
        $searchq = $_POST['search'];
    $query = mysql_query("SELECT * FROM members WHERE firstname LIKE '%searchq%'" ) or die("could not search");
    $count = mysql_num_rows($query);
    if($count == 0){
        $output = 'There was no search results !';
        }else{
        while($row = mysql_fetch_array($query)){
        $fname = $row['firstname'];
        $output .='<div> '.$fname.'</div>';
        }

        }
}


?>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>search</title>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="search" placeholder="search for members"/> 
<input type="submit" value=">>"/>
</form>
<?php print("$output);?>
</body
</html> 

for example i type andre in and i get the response

There was no search results !

could someone please help

Best Answer

The problem come from this line of your code

$query = mysql_query("SELECT * FROM members WHERE firstname LIKE '%searchq%'" )

the variable searchq doesnot have $ at the back