![](/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.