Current location: Hot Scripts Forums » Programming Languages » PHP » Having a problem with JOINS in my code - Help!


Having a problem with JOINS in my code - Help!

Reply
  #1 (permalink)  
Old 01-26-10, 02:02 PM
CountryGirlDesigns CountryGirlDesigns is offline
Newbie Coder
 
Join Date: Jan 2010
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Having a problem with JOINS in my code - Help!

I seem to be having a problem trying to join in multiple tables. I was using the INNER JOIN clause since yesterday and it was working great. I had several tables joined in and everything seemed fine. I tried to add in another table and change some of the way the results returned as and then it wouldn't retrieve anything. So, I started rewriting the whole code to the point where it had last worked and now it won't work at all.

It's working fine pulling information from ONE table, but when I try to join in any other table, it doesn't work. What am I doing wrong??? Please help!

Here is the code I was using with all JOINs and it was working fantastic until right now. Do you see anything in it that shouldn't be in it??

Code:
<?php
   
   $dbHost = '********'; 
   $dbUser = '******'; 
   $dbPass = '********';
   $dbDatabase = '*****'; 
  

$search = $_POST['search'];

if ($search) // perform search only if a string was entered.
{
   $con = mysql_connect($dbHost, $dbUser, $dbPass) or die("Failed to connect to MySQL Server. Error: " . mysql_error());
   mysql_select_db($dbDatabase) or die("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());
   
   
   $query = "SELECT asmnt_parcel.Account, asmnt_parcel.OwnersName, asmnt_parcel.ParcelID, asmnt_parcel.Township, asmnt_parcel.Range, asmnt_parcel.Section, asmnt_parcel.LotSize, asmnt_parcel.LotSizeType, asmnt_parcel.TaxAreaCode, asmnt_legal.Legal, cmn_name.Address2, cmn_name.City, cmn_name.State, cmn_name.ZipCode, asmnt_situs.Situs 
 			 INNER JOIN asmnt_legal ON asmnt_parcel.Account=asmnt_legal.Account
 			 INNER JOIN cmn_name ON asmnt_parcel.OwnersName=cmn_name.OwnersName
 			 INNER JOIN asmnt_situs ON asmnt_parcel.Account=asmnt_situs.Account
   			 WHERE asmnt_parcel.Account = '{$search}' OR asmnt_parcel.OwnersName = '{$search}' OR asmnt_parcel.ParcelID = '{$search}' OR asmnt_legal.Legal = '{$search}'
             ORDER BY asmnt_parcel.Account";
   $result = mysql_query($query, $con);

   if ($result)
   {
      echo "Results:<br><br>";
      echo "<table width=90% align=center border=1><tr>
      <td align=center bgcolor=#4A6B3F>Account</td>
      <td align=center bgcolor=#4A6B3F>Owners Name</td>
      <td align=center bgcolor=#4A6B3F>Address</td>
      <td align=center bgcolor=#4A6B3F>City</td>
      <td align=center bgcolor=#4A6B3F>State</td>
      <td align=center bgcolor=#4A6B3F>Zip Code</td>
      <td align=center bgcolor=#4A6B3F>Legal</td>
      <td align=center bgcolor=#4A6B3F>Parcel ID</td>
      <td align=center bgcolor=#4A6B3F>Township</td>
      <td align=center bgcolor=#4A6B3F>Range</td>
      <td align=center bgcolor=#4A6B3F>Section</td>
      <td align=center bgcolor=#4A6B3F>Size</td>
      <td align=center bgcolor=#4A6B3F>Type</td>
      <td align=center bgcolor=#4A6B3F>School District</td>
      <td align=center bgcolor=#4A6B3F>Situs</td>
      </tr>";
   
      while ($r = mysql_fetch_array($result))
      { // Begin while
         $act = $r["Account"]; 
         $nme = $r["OwnersName"];
         $add = $r["Address2"];  
         $city = $r["City"];  
         $ste = $r["State"];  
         $zip = $r["ZipCode"]; 
         $legal = $r["Legal"];   
         $pid = $r["ParcelID"];
         $tship = $r["Township"]; 
         $rng = $r["Range"];
         $sctn = $r["Section"];   
         $size = $r["LotSize"];  
         $type = $r["LotSizeType"]; 
         $sch = $r["TaxAreaCode"];
         $sit = $r["Situs"];
         echo "<tr>
            <td>$act</td>
            <td>$nme</td>
            <td>$add</td>
            <td>$city</td>
            <td>$ste</td>
            <td>$zip</td>
            <td>$legal</td>
            <td>$pid</td>
            <td>$tship</td>
            <td>$rng</td>
            <td>$sctn</td>
            <td>$size</td>
            <td>$type</td>
            <td>$sch</td>
            <td>$sit</td>
            </tr>";
      } // end while
      
      echo "</table>";
   }
   else
   {
      echo "Sorry, please try your search again.";
   }
}
else
{
   echo "Start your search";
}
?>
This is what I've got with only pulling from one table right now that is working:

Code:
<?php
   
   $dbHost = '***********'; 
   $dbUser = '*******'; 
   $dbPass = '*********';
   $dbDatabase = '*********'; 
  

$search = $_POST['search'];

if ($search) // perform search only if a string was entered.
{
   $con = mysql_connect($dbHost, $dbUser, $dbPass) or die("Failed to connect to MySQL Server. Error: " . mysql_error());
   mysql_select_db($dbDatabase) or die("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());
   
   
   $query = "SELECT OwnersName
   			 FROM asmnt_parcel
   			 WHERE asmnt_parcel.OwnersName = '{$search}' 
             ORDER BY asmnt_parcel.OwnersName ASC";
   $result = mysql_query($query, $con);

   if ($result)
   {
      echo "Results:<br><br>";
      echo "<table width=90% align=center border=1><tr>
      <td align=center bgcolor=#4A6B3F>Owners Name</td>
      </tr>";
   
      while ($r = mysql_fetch_array($result))
      { // Begin while
         $name = $r["OwnersName"];  
         echo "<tr>
            <td>$name</td>
            </tr>";
      } // end while
      
      echo "</table>";
   }
   else
   {
      echo "Sorry, please try your search again.";
   }
}
else
{
   echo "Start your search";
}
?>
Thanks!
Qadoshyah
Reply With Quote
  #2 (permalink)  
Old 01-26-10, 03:53 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
It's probably simpler to just run more than one select statement.

You're searching the asmnt_parcel and asmnt_legal tables. Search one table, then the next if you don't find anything. Once you have results, select the remaining data from the other tables.

Instead of JOIN, you could also use FROM -

SELECT a.*, b.*, c.* FROM asmnt1 AS a ... WHERE a.id=id

Not tested.
Reply With Quote
  #3 (permalink)  
Old 01-26-10, 07:21 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
UN-TESTED

PHP Code:

$query "SELECT asmnt_parcel.Account, asmnt_parcel.OwnersName, asmnt_parcel.ParcelID, asmnt_parcel.Township, asmnt_parcel.Range, asmnt_parcel.Section, asmnt_parcel.LotSize, asmnt_parcel.LotSizeType, asmnt_parcel.TaxAreaCode, asmnt_legal.Legal, cmn_name.Address2, cmn_name.City, cmn_name.State, cmn_name.ZipCode, asmnt_situs.Situs 
              INNER JOIN asmnt_legal ON asmnt_parcel.Account=asmnt_legal.Account
              INNER JOIN cmn_name ON asmnt_parcel.OwnersName=cmn_name.OwnersName
              INNER JOIN asmnt_situs ON asmnt_parcel.Account=asmnt_situs.Account
                WHERE asmnt_parcel.Account = '
{$search}' OR asmnt_parcel.OwnersName = '{$search}' OR asmnt_parcel.ParcelID = '{$search}' OR asmnt_legal.Legal = '{$search}'
             ORDER BY asmnt_parcel.Account"
;
             
//Should be (UNTESTED).

$query "SELECT asmnt_parcel.Account,
                            asmnt_parcel.OwnersName,
                            asmnt_parcel.ParcelID, 
                            asmnt_parcel.Township, 
                            asmnt_parcel.Range, 
                            asmnt_parcel.Section, 
                            asmnt_parcel.LotSize, 
                            asmnt_parcel.LotSizeType, 
                            asmnt_parcel.TaxAreaCode, 
                            asmnt_legal.Legal, 
                            cmn_name.Address2, 
                            cmn_name.City, 
                            cmn_name.State, 
                            cmn_name.ZipCode, 
                            asmnt_situs.Situs
                FROM     asmnt_parcel,
                            asmnt_legal,
                            cmn_name,
                            asmnt_situs
                WHERE (asmnt_parcel.Account = asmnt_legal.Account
                AND        asmnt_parcel.OwnersName = cmn_name.OwnersName
                AND     asmnt_parcel.Account = asmnt_situs.Account)
                AND     (asmnt_parcel.Account = '
{$search}'
                OR        asmnt_parcel.OwnersName = '
{$search}'
                OR        asmnt_parcel.ParcelID = '
{$search}'
                OR        asmnt_legal.Legal = '
{$search}')
                ORDER BY asmnt_parcel.Account"

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP inside JavaScript? pikaz JavaScript 20 07-10-10 12:55 AM
Captcha Code not working Frogger PHP 0 12-01-09 01:29 PM
Code behind problem.. shabirmaher ASP.NET 1 08-12-05 08:11 AM
How to sale php code to customer without giving him code pradeep_soft PHP 4 03-12-04 12:10 PM


All times are GMT -5. The time now is 08:09 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.