aldrin151 Posted June 23, 2008 Share Posted June 23, 2008 Hi there...I sure someone can help me out with a simple php search script. Basically I can't get the script to yield the correct results. Below is the entire code...My dilema is when using $result = mysql_query("SELECT * FROM employer_posts WHERE title like " . "'%$title%'" . "OR description like " . "'%$title%'" . "AND category=" . "'$category'" . ""); I get the query yielding results for keyword that's like so and so in either title or description but I don't get results for category..., but when using $result = mysql_query("SELECT * FROM employer_posts WHERE title like " . "'%$title%'" . "AND description like " . "'%$title%'" . "AND category=" . "'$category'" . "");......Notice AND AND and not OR AND I get the query yielding results for keyword that's like so and so in title and description and category... Any suggestions in using the correct sql statment in this case...I greatly appreciate it! ??? <html> <body> <form action="search_results.php" method="get"> <input name="title" type="text"> <select name="category" size="1"> <option>Bar Tender</option> <option>Disk Jockey</option> </select> <input type="submit" value="Search" /> </form> </body> </html> <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("postdb", $con); $title = $_GET["title"]; $category = $_GET["category"]; $result = mysql_query("SELECT * FROM employer_posts WHERE title like " . "'%$title%'" . "OR description like " . "'%$title%'" . "AND category=" . "'$category'" . ""); while($row = mysql_fetch_array($result)) { echo $row['title']; echo "<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/111581-php-search-query/ Share on other sites More sharing options...
miracle_potential Posted June 24, 2008 Share Posted June 24, 2008 Try this instead of like because LIKE is an SQL command I'm pretty sure ( I should know I use it everyday ??? ) $result = mysql_query("SELECT title FROM employer_posts WHERE title='" . %$title% . "' OR description='" . %title% ."'")or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/111581-php-search-query/#findComment-572750 Share on other sites More sharing options...
aldrin151 Posted June 24, 2008 Author Share Posted June 24, 2008 Hi...thanks for the help...I just tried $result = mysql_query("SELECT title FROM employer_posts WHERE title='" . %$title% . "' OR description='" . %$title% ."'")or die(mysql_error()); I got a script error Parse error: syntax error, unexpected '%' in C:\xampp\htdocs\westsidetemps\test\search_results.php on line 31 but tried $result = mysql_query("SELECT title FROM employer_posts WHERE title='" . "'%$title%'" . "' OR description='" . "'%$title%'" ."'")or die(mysql_error()); I get a Unknown column 'whatever' in 'where clause' when I run a search for say "whatever" Link to comment https://forums.phpfreaks.com/topic/111581-php-search-query/#findComment-572759 Share on other sites More sharing options...
DarkWater Posted June 24, 2008 Share Posted June 24, 2008 $result = mysql_query("SELECT title FROM employer_posts WHERE title='%" . $title . "%' OR description='%" . $title ."%'")or die(mysql_error()); This is why we use sprintf() for queries. =X Link to comment https://forums.phpfreaks.com/topic/111581-php-search-query/#findComment-572768 Share on other sites More sharing options...
aldrin151 Posted June 24, 2008 Author Share Posted June 24, 2008 Hi DartWater...I just tried $result = mysql_query("SELECT title FROM employer_posts WHERE title='%" . $title . "%' OR description='%" . $title ."%'")or die(mysql_error()); this time I get no results when I run the search...no records returned...here is the full script <html> <body> <form action="search_results.php" method="get"> <input name="title" type="text"> <select name="category" size="1"> <option>Bar Tender</option> <option>Disk Jockey</option> </select> <input type="submit" value="Search" /> </form> </body> </html> <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("portdb", $con); $title = $_GET["title"]; $category = $_GET["category"]; $result = mysql_query("SELECT title FROM employer_posts WHERE title='%" . $title . "%' OR description='%" . $title ."%'")or die(mysql_error()); while($row = mysql_fetch_array($result)) { echo $row['title']; echo "<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/111581-php-search-query/#findComment-572772 Share on other sites More sharing options...
tpimental Posted June 24, 2008 Share Posted June 24, 2008 I think you just need some parenthesis in your logic... try something like this: $result = mysql_query("SELECT * FROM employer_posts WHERE (title like " . "'%$title%'" . "OR description like " . "'%$title%'" . ") AND category=" . "'$category'" . ""); Link to comment https://forums.phpfreaks.com/topic/111581-php-search-query/#findComment-572774 Share on other sites More sharing options...
aldrin151 Posted June 24, 2008 Author Share Posted June 24, 2008 tpimental, you are awesome....I was thinking along the lines before as if I should include the parenthesis, just couldn't put the statement together Can you let me know how I could do the same thing with the $swhere statment below? $sWhere = " WHERE title ". "like " . tosql("%".get_param("title") ."%", "Text") . " OR description ". "like " . tosql("%".get_param("title") ."%", "Text") . " AND category=" . tosql(get_param("category"), "Text"); Thanks so much Link to comment https://forums.phpfreaks.com/topic/111581-php-search-query/#findComment-572818 Share on other sites More sharing options...
aldrin151 Posted June 24, 2008 Author Share Posted June 24, 2008 Got it! I really appreciate it...works perfect... $sWhere = " WHERE (title ". "like " . tosql("%".get_param("title") ."%", "Text") . " OR description ". "like " . tosql("%".get_param("title") ."%", "Text") . ") AND category=" . tosql(get_param("category"), "Text"); Link to comment https://forums.phpfreaks.com/topic/111581-php-search-query/#findComment-572822 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.