[英]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.我正在用html
和php
编写一个数据库,但我无法搜索数据。 My code is as follows:我的代码如下:
HTML: 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:
<?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.
结果页面显示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.我把床放在数据库中,就像选择值一样,数字和 BHK 单词之间有空格。 So I had to comment the preg_replace expression to keep that space.所以我不得不注释 preg_replace 表达式以保留该空间。
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.否则,只需回显 $searchq 变量以查看它如何并使其与数据库中的值相似。 I hope you understand what I mean.我希望你明白我的意思。
And try to update your code to use the new MySQLi or PDO extensions of PHP.并尝试更新您的代码以使用 PHP 的新 MySQLi 或 PDO 扩展。 Because mysql expressions are deprecated and will be removed in the futur as it's mentioned in the php manual website.因为 mysql 表达式已被弃用,并且将在未来被删除,正如 php 手册网站中提到的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.