osyrys14 Posted March 27, 2013 Share Posted March 27, 2013 Hey everyone, I found a simple search online, and it's acting strange. If I put anything in the search field, it returns nothing, if I leave it blank, it returns every field in the database. Code is below if anyone can check... I've been staring at it for over 2 hours trying to find out why... Thanks in advance. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Search</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <form method="get" action=""> <label>Search For: </label><input type="text" name="query" /> <input type="submit" name="submit" value="Start Search" /> <input type="reset" value="Reset" </form> <?php if(isset($_GET['submit'])){ $db_host="localhost"; $db_username="myusername"; $db_password="mypass"; $db_name="nydb"; $db_tb_name="mytable"; $db_tb_atr_name="column"; mysql_connect("$db_host","$db_username","$db_password"); mysql_select_db("$db_name"); $query=mysql_real_escape_string($_GET['query']); $query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE '8' like '%".$query."%'"); echo "<h2>Search Results</h2><ol>"; while($data_fetch=mysql_fetch_array($query_for_result)) { echo "<li>"; echo substr($data_fetch[$db_tb_atr_name], 0,160); echo "</li><hr/>"; } echo "</ol>"; mysql_close(); } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/276199-simple-search-not-working-right/ Share on other sites More sharing options...
Solution Barand Posted March 27, 2013 Solution Share Posted March 27, 2013 ... WHERE '8' like '%".$query."%'") What are you putting in $query with a hope of getting records returned which match the string '8' ? If you actually have a column named "8" (ridiculous, I know) then it should be in backticks, not quotes. Quote Link to comment https://forums.phpfreaks.com/topic/276199-simple-search-not-working-right/#findComment-1421280 Share on other sites More sharing options...
osyrys14 Posted March 27, 2013 Author Share Posted March 27, 2013 I knew I was missing something simple! That fixed it, thank you so much for the help!! Quote Link to comment https://forums.phpfreaks.com/topic/276199-simple-search-not-working-right/#findComment-1421285 Share on other sites More sharing options...
osyrys14 Posted March 27, 2013 Author Share Posted March 27, 2013 one more quick question.... This code is where it's putting the information out, echo "<li>"; echo substr($data_fetch[$db_tb_atr_name], 0,160); echo "</li><hr/>"; How easy is it to add more of the data (since I'm pulling * from database), to the output, and one of them is a number field that we need to add together for the sum of all rows. I really appreciate your help! Quote Link to comment https://forums.phpfreaks.com/topic/276199-simple-search-not-working-right/#findComment-1421293 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.