简体   繁体   中英

Unable to fetch data from MySQL using PHP

I'm coding a database in html and php but I am unable to search for data. My code is as follows:

HTML:

<body>
    <form action="try.php" method="post">
        <select name="search" id="search">
        <option>1 BHK</option>
        <option>2 BHK</option>
        <option>3 BHK</option>
        <option>4 BHK</option>
        <option>5 BHK</option>
    </select><input type="submit" value="search" >
    </form>
    <?php print("$output"); ?>
</body>

PHP:

<?php

mysql_connect("localhost","root","") or die("access denide");
mysql_select_db("destini_homes") or die("no databse found");
$output = '';
if(isset($_POST['search'])){

$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysql_query("SELECT * FROM destinyhomes_data WHERE beds LIKE     '%$searchq%'")or die("couled not find results matching your search"); 
$count = mysql_num_rows($query);
if($count == 0){
    $output = 'there was no search result matching to your search';
}
else{
    while($row = mysql_fetch_array($query)){
        $beds = $row['beds'];
        $city = $row['city'];
        $property_type = $row['property_type'];
        $locality = $row['locality'];
        $complex = $row['complex'];
        $note = $row['note'];

        $output .='<div>'.$beds.''.$city.'</div>';
        }
    }

}

?>

The result page displays There was no search result matching to your search.

What is wrong with my approach?

Here's a code that I used and it worked :

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<?php

mysql_connect("localhost","root","") or die("access denide");
mysql_select_db("destini_homes") or die("no databse found");
$output = '';
if(isset($_POST['search'])){

$searchq = $_POST['search'];
//$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
echo ($searchq);
$query = mysql_query("SELECT * FROM destinyhomes_data WHERE beds LIKE '%$searchq%'")or die("could not find results matching your search"); 
$count = mysql_num_rows($query);
if($count == 0){
    $output = 'there was no search result matching to your search';
}else{
    while($row = mysql_fetch_array($query)){
        $beds = $row['beds'];
        $city = $row['city'];
        $property_type = $row['property_type'];
        $locality = $row['locality'];
        $complex = $row['complex'];
        $note = $row['note'];

        $output .='<div>'.$beds.''.$city.'</div>';
        }
    }

}

?>
<form action="try.php" method="post">
<select name="search" id="search">
<option>1 BHK</option>
<option>2 BHK</option>
<option>3 BHK</option>
<option>4 BHK</option>
<option>5 BHK</option>
</select><input type="submit" value="search" >
</form>
<?php echo("$output"); ?>
</body>
</html>

I put the beds in the database to be like the select values exactly , with the space between the number and the BHK word. So I had to comment the preg_replace expression to keep that space.

See if you're making the same as well (if you're putting the beds values with the space).

Otherwise, just echo the $searchq variable to see how is it and make it like the values in your database. I hope you understand what I mean.

And try to update your code to use the new MySQLi or PDO extensions of PHP. Because mysql expressions are deprecated and will be removed in the futur as it's mentioned in the php manual website.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM