jjacquay712 Posted October 3, 2008 Share Posted October 3, 2008 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? Link to comment https://forums.phpfreaks.com/topic/126938-solved-wrong-order-of-operations-in-php-genorated-sql-query/ Share on other sites More sharing options...
phppucci Posted October 3, 2008 Share Posted October 3, 2008 use () there is some (and | or) operator priority Link to comment https://forums.phpfreaks.com/topic/126938-solved-wrong-order-of-operations-in-php-genorated-sql-query/#findComment-656605 Share on other sites More sharing options...
jjacquay712 Posted October 3, 2008 Author Share Posted October 3, 2008 can you give an example? i don't really understand Link to comment https://forums.phpfreaks.com/topic/126938-solved-wrong-order-of-operations-in-php-genorated-sql-query/#findComment-656609 Share on other sites More sharing options...
phppucci Posted October 3, 2008 Share Posted October 3, 2008 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%') Link to comment https://forums.phpfreaks.com/topic/126938-solved-wrong-order-of-operations-in-php-genorated-sql-query/#findComment-656655 Share on other sites More sharing options...
jjacquay712 Posted October 3, 2008 Author Share Posted October 3, 2008 Thanks! That did the trick! Link to comment https://forums.phpfreaks.com/topic/126938-solved-wrong-order-of-operations-in-php-genorated-sql-query/#findComment-656671 Share on other sites More sharing options...
Barand Posted October 3, 2008 Share Posted October 3, 2008 MySQL needs to know if WHERE A AND B OR C means WHERE A AND (B OR C) or WHERE (A AND B) OR C Link to comment https://forums.phpfreaks.com/topic/126938-solved-wrong-order-of-operations-in-php-genorated-sql-query/#findComment-656685 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.