Jump to content

[SOLVED] Query Help - Making me nuts


inspireddesign

Recommended Posts

Hello there,

 

For whatever reason my query is displaying the same result over-&-over again in the loop.  It's not a continues loop.  It acts like there are several of the same entries in the database when there is only one entry.  What might be causing this?  I would think it has to do with the JOIN method I'm using but not sure? 

 

Thanks for any help on this.  Let me know if you need more information (code).

 

<?php
	  $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
	  	
	  $searchSQL  = "SELECT * FROM contacts ";
	  $searchSQL .= "LEFT JOIN policies ON (policies.policy_contact = contacts.contact_id) ";
	  $searchSQL .= "LEFT JOIN aircraft ON (aircraft.aircraft_contact = contacts.contact_id) ";
	  $searchSQL .= "LEFT JOIN airport ON (airport.airport_contact = contacts.contact_id) ";
	  $searchSQL .= "LEFT JOIN product ON (product.product_contact = contacts.contact_id) ";
	  $searchSQL .= "WHERE policies.policy_num LIKE '%{$searchTermDB}%' ";
	  $searchSQL .= "OR aircraft.aircraft_n LIKE '%{$searchTermDB}%' ";
	  $searchSQL .= "OR contacts.contact_primary LIKE '%{$searchTermDB}%' OR contacts.contact_zip LIKE '%{$searchTermDB}%' ";   

?>

Link to comment
https://forums.phpfreaks.com/topic/167984-solved-query-help-making-me-nuts/
Share on other sites

try distinct

 

$searchSQL  = "SELECT DISTINCT * FROM contacts ";
        $searchSQL .= "LEFT JOIN policies ON (policies.policy_contact = contacts.contact_id) ";
        $searchSQL .= "LEFT JOIN aircraft ON (aircraft.aircraft_contact = contacts.contact_id) ";
        $searchSQL .= "LEFT JOIN airport ON (airport.airport_contact = contacts.contact_id) ";
        $searchSQL .= "LEFT JOIN product ON (product.product_contact = contacts.contact_id) ";
        $searchSQL .= "WHERE policies.policy_num LIKE '%{$searchTermDB}%' ";
        $searchSQL .= "OR aircraft.aircraft_n LIKE '%{$searchTermDB}%' ";
        $searchSQL .= "OR contacts.contact_primary LIKE '%{$searchTermDB}%' OR contacts.contact_zip LIKE '%{$searchTermDB}%' "; 

 

Without it, your query should return the same results multiple times if I understand it right. This is due to your OR statements.

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.