无法使用PHP从MySQL获取数据

时间:2015-08-02 15:26:19

标签: php html mysql

我正在htmlphp对数据库进行编码,但我无法搜索数据。我的代码如下:

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>';
        }
    }

}

?>

结果页面显示There was no search result matching to your search.

我的做法出了什么问题?

1 个答案:

答案 0 :(得分:0)

这是我使用过的代码,它起作用了:

<!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>

我把数据库中的床确切地说成了选择值,数字和BHK字之间的空格。所以我不得不评论preg_replace表达式以保留该空间。

看看你是否也做同样的事情(如果你把床位值与空间放在一起)。

否则,只需回显$ searchq变量即可查看它是如何使其像数据库中的值一样。我希望你明白我的意思。

尝试更新代码以使用PHP的新MySQLi或PDO扩展。因为不推荐使用mysql表达式,并且将在php手册网站中提到的未来中删除它们。