![](/img/trans.png)
[英]Unable to fetch data from MySQL database using PHP in Android Studio
[英]Unable to fetch data from MySQL using PHP
我正在用html
和php
编写一个数据库,但我无法搜索数据。 我的代码如下:
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.
我的方法有什么问题?
这是我使用的代码并且它有效:
<!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 手册网站中提到的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.