Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.