spearchilduser Posted January 10, 2012 Share Posted January 10, 2012 hi im just trying to do a search on a database and display results based on what the user selected: form code: html> <body> <form action="search.php" method="POST"> Type of property: <select name="Type_of_Property"> <option>Terraced</option> <option>Detached</option> <option>Semi-Detached</option> <option>Bungalow</option> <option>Flat </option> </select> <p><input type="submit" value="Send Details" name="B1"></p> </form> php code: <html> <body> <?php error_reporting(E_ALL); mysql_connect("localhost", "root") or die(mysql_error()); // makes a connection mysql_select_db("estate_agents") or die('I cannot connect to the database because: ' . mysql_error()); //conects to the database $result = mysql_query("SELECT * FROM properties WHERE Type_of_Property ='".$_POST['Type_of_Property']); echo "<table border=1>\n"; echo "<tr><td>Address 1</td><td>Address 2</td><td>Postcode</td><td>Type of Property</td><td>Number of Bedrooms</td><td>Number of Bathrooms</td></tr>\n"; while ($row = mysql_fetch_row($result)) { } print "Data base updated with: " .$_POST["Type_of_Property"] ; ?> </body> </html> the propbelm im having is being able to display the results in the table in the while loop any help is appreciated thank you Quote Link to comment https://forums.phpfreaks.com/topic/254712-search-function/ Share on other sites More sharing options...
Muddy_Funster Posted January 10, 2012 Share Posted January 10, 2012 use mysql_fetch_array() or mysql_fetch_assoc() not mysql_fetch_row(). If you use mysql_fetch_assoc() then you will need to use the actual column names within the square brackets and single quotes rather then the numerical array key value. while ($row = mysql_fetch_array($result)) { echo"<tr><td>{$row['0']}</td><td>{$row['1']}</td>...<td>{$row['n']}</td></tr>"; }// remember to close your table after the results are written out Quote Link to comment https://forums.phpfreaks.com/topic/254712-search-function/#findComment-1306049 Share on other sites More sharing options...
spearchilduser Posted January 10, 2012 Author Share Posted January 10, 2012 Thanx that makes scence however it tells me the Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\homes\search.php on line 14 which is the while loop while ($row = mysql_fetch_array($result)) { Thanx Quote Link to comment https://forums.phpfreaks.com/topic/254712-search-function/#findComment-1306054 Share on other sites More sharing options...
spearchilduser Posted January 10, 2012 Author Share Posted January 10, 2012 The data being retireved is words not numerical and the code actually works if i take away the while loop statement but it looks liek it is written correclty :s Quote Link to comment https://forums.phpfreaks.com/topic/254712-search-function/#findComment-1306061 Share on other sites More sharing options...
Muddy_Funster Posted January 10, 2012 Share Posted January 10, 2012 what's the output of print_r($result) you should get something like "Resource #5" also, you have no error capture on your actual query, use an or die ("error:".mysql_error()) on that line too. Quote Link to comment https://forums.phpfreaks.com/topic/254712-search-function/#findComment-1306063 Share on other sites More sharing options...
spearchilduser Posted January 10, 2012 Author Share Posted January 10, 2012 The output is the users entry on the entry form i was just usign it to make sure the correct data was beign passed through the Post function; However the error that has come up when i put the error checking in is error:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Terraced' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/254712-search-function/#findComment-1306065 Share on other sites More sharing options...
spearchilduser Posted January 10, 2012 Author Share Posted January 10, 2012 Sorry mis read what you said the output is resource id#4 Quote Link to comment https://forums.phpfreaks.com/topic/254712-search-function/#findComment-1306077 Share on other sites More sharing options...
litebearer Posted January 10, 2012 Share Posted January 10, 2012 1. missing closing quote 2. put queries in string so you can echo for debugging try changing this... $result = mysql_query("SELECT * FROM properties WHERE Type_of_Property ='".$_POST['Type_of_Property']); to this... $search_for = .$_POST['Type_of_Property']); $query = "SELECT * FROM properties WHERE Type_of_Property ='search_for'"; $result = mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/254712-search-function/#findComment-1306081 Share on other sites More sharing options...
Muddy_Funster Posted January 10, 2012 Share Posted January 10, 2012 1. missing closing quote 2. put queries in string so you can echo for debugging try changing this... $result = mysql_query("SELECT * FROM properties WHERE Type_of_Property ='".$_POST['Type_of_Property']); to this... $search_for = .$_POST['Type_of_Property']); $query = "SELECT * FROM properties WHERE Type_of_Property ='search_for'"; $result = mysql_query($query); You missed the $ off the start of search_for in the $query string. Quote Link to comment https://forums.phpfreaks.com/topic/254712-search-function/#findComment-1306135 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.