PHP:搜索时查询为空

时间:2013-08-04 13:35:43

标签: php mysql

我的问题需要一些帮助...

场景:我试图在我的数据库中搜索某些内容,但结果为"查询为空"但是我试图搜索的那个已经在我的数据库中了。我试图搜索" Atrium Hotel"

这是我的数据库截图: enter image description here

这是我的结果页面截图:

enter image description here

最后,这是我的代码:

<input type='submit' name='search' value='Search Building' onClick="this.form.action='search_bldg.php'; this.form.submit()">
<input type="text" id="idSearch"name="searchBldg" size="40" value="<?php echo $_POST['searchBldg']; ?>">        
<fieldset  width= "50px">
<legend>BUILDING/S</legend>
<?php
$search = $_POST["searchBldg"];
$data = mysql_query("SELECT * FROM tbldata WHERE fldBldgName LIKE '%$search%'"); 
$result = mysql_query($data) or die(mysql_error());
while($row = mysql_fetch_array( $result )) 
{ 
echo $row['fldBldgName']; 

} 
?>
</fieldset>

我想知道我的查询中有什么问题...... 提前谢谢......

3 个答案:

答案 0 :(得分:1)

您正在执行两次查询(第7行和第8行)。这可能是问题所在。尝试这样的事情:

<input type='submit' name='search' value='Search Building' onClick="this.form.action='search_bldg.php'; this.form.submit()">
<input type="text" id="idSearch"name="searchBldg" size="40" value="<?php echo $_POST['searchBldg']; ?>">        
<fieldset  width= "50px">
<legend>BUILDING/S</legend>
<?php
$search = $_POST["searchBldg"];
$query= "SELECT * FROM tbldata WHERE fldBldgName LIKE '%$search%'"; //Your sql
$result = mysql_query($query) or die(mysql_error()); //execute your query
while($row = mysql_fetch_array( $result )) 
{ 
echo $row['fldBldgName']; 

} 
?>
</fieldset>

P.S。使用mysqli_*PDO代替mysql_*,因为自PHP 5.4起已弃用

答案 1 :(得分:0)

您应该使用mysqliPDO,因为mysql_*已弃用。

尝试以下代码:使用mysqli。它应该工作......

<?php
$search = $_POST["searchBldg"];

//connecting to db...mysqli_connect("example.com","peter","abc123","my_db")
$con=mysqli_connect(host,username,password,dbname);

//searching...
$query= "SELECT * FROM tbldata WHERE fldBldgName LIKE '%$search%'"; 

//execute the query
$result = mysqli_query($query) or die(mysqli_error()); 
while($row = mysqli_fetch_array( $result )) 
{ 
echo $row['fldBldgName']; 

} 
mysqli_close($con);
?>

答案 2 :(得分:0)

您的代码中存在一些问题,我将通过评论您的代码来演示:

<input type='submit' name='search' value='Search Building' action='search_bldg.php'  onClick="this.form.submit();"> <!-- setting action-attribute directly in form-tag, no need to use js for that -->
<input type="text" id="idSearch" name="searchBldg" size="40" value="<?php echo $_POST['searchBldg']; ?>">  <!-- added a space between id="search" and name-attribute -->
<fieldset  width= "50px">
<legend>BUILDING/S</legend>
<?php
$search = $_POST["searchBldg"];
$data = "SELECT * FROM tbldata WHERE fldBldgName LIKE '%$search%'"; //mysql_query is removed, because the actual query is executed below 
$result = mysql_query($data) or die(mysql_error());
while($row = mysql_fetch_assoc( $result )) //mysql_fetch_array doesn't return associative arrays, therefore it's replaced with mysql_fetch_assoc 
{ 
echo $row['fldBldgName']; 

} 
?>

</fieldset>

您当然应该清理数据(因此不会将不需要的数据插入到数据库中),并使用mysql_ *之外的其他内容,如前面的答案中所述。

另一个问题(这不是一个真正的问题)是元素的名称,列名等.searchBldg与searchB1dg非常相似,并且可能很容易进行输入 - 可能很难找到的错误。