Jump to content

[SOLVED] Wrong Order Of Operations In PHP Genorated SQL Query


jjacquay712

Recommended Posts

Ok.... heres my problem. I have a php script that generates an SQL query to search a database. Here is my script:

<?php
//connect
mysql_connect("**********", "********", "*******");
mysql_select_db("********");
//End Connect
if ($_GET['query']) {
$array = explode(" ", $_GET['query']);
$num = count($array) - 1;
$query = "SELECT title,description,link FROM search WHERE title like '%" . $array[$num] . "%' or description like '%" . $array[$num] . "%' or link like '%" . $array[$num]. "%' ";
$num--;
while ( $num >= 0 ) {
	$query .= " and title like '%" . $array[$num] . "%' or description like '%" . $array[$num] . "%' or link like '%" . $array[$num]. "%'";
	$num--;
}
$rows = mysql_num_rows(mysql_query($query));
if ( $rows > 0 ) {
	$query2 = mysql_query($query);
	while ( $row2 = mysql_fetch_array($query2) ) {
		echo $row2["title"] . "<br />" . $row2["link"] . "<br />" . $row2["description"];

	}
} else {
	echo $query;
	echo "<br />No Results";
}
echo "<br /><br />" . $query;
} else {
echo "Please go back an enter search query.";
}



?>

 

The generated query looks like this when i enter the search term "test", and "search":

 

SELECT title,description,link FROM search WHERE title like '%test%' or description like '%test%' or link like '%test%' and title like '%search%' or description like '%search%' or link like '%search%'

 

My problem is that it always returns a result even if the second AND statement is evaluated false. I have talked to a few people about this and they said my "order of operations" are wrong in the query. Can someone explain how i could do the query different to get the desired results?

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.