Jump to content

PHP Search Query


aldrin151

Recommended Posts

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

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

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

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

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

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.