0

I need a little help for my problem...

Scene: I was trying to search something in my database but the result is "Query was empty" but the one I'm trying to search is already in my database. I'm trying to search the "Atrium Hotel"

Here's my screenshot of my Database: enter image description here

Here's my screenshot of my result Page:

enter image description here

And Lastly here's my code:

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

I was wondering what is the problem in my query... Thanks in advance...

4
  • 3
    Don't use mysql - it's deprecated. Use mysqli or PDO. Also, your code is open to SQL inhection attack. Lastly, your code here is incomplete: nowhere does it emit Query was empty. Commented Aug 4, 2013 at 13:45
  • Like @MikeW said. Plus, are you sure the table and field name are both correct? Try dumping your query using var_dump("SELECT * FROM tbldata WHERE fldBldgName LIKE '%$search%'") and see what the result is. Maybe there's just a logical error? Commented Aug 4, 2013 at 13:47
  • @UrGuardian4ngel...Here's the result with var_dump "string(61) "SELECT * FROM tbldata WHERE fldBldgName LIKE '%Atrium Hotel%'" Query was empty".... and the table and field name are correct. Commented Aug 4, 2013 at 13:49
  • @PhilistyneBrigidBellisima better to provide the sqlfiddle example with your sample data + query Commented Aug 4, 2013 at 14:01

3 Answers 3

1

You are executing your query twice (Line #7 and #8). That may be the problem. Try something like this:

<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. use mysqli_* or PDO instead of mysql_* since it is deprecated as of PHP 5.4

Sign up to request clarification or add additional context in comments.

2 Comments

Am I missing something here? Shouldn't it be mysql_fetch_assoc ?
You can either use mysql_fetch_array or mysql_fetch_assoc. mysql_fetch_assoc returns an "associative array". It could be a better choice. You can read about the difference here - stackoverflow.com/questions/9540483/…
0

You should use mysqli or PDO since mysql_* is depreciated.

Try the below code: It's using mysqli . It should be working...

<?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);
?>

Comments

0

There are some issues in your code that I'll demonstrate by commenting your code:

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

You should of course sanitize data (so not unwanted data gets inserted into db) and use something else besides mysql_* as stated in previous answers.

Another issue (which is not really a problem) is the name of elements, column-names etc. searchBldg is very similar to searchB1dg and it might be easy to do typing-errors which might be hard to find.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.