0

I have two tables (claim_pics & claiminfo) I am doing a query for a results page.

Its works well.. when there there is a match of the two tables claim_id ( claiminfo.claim_id = claim_pics.claim_id ).

However when there there is only one table with a claim_id, namely in claiminfo, and not a matching claim_id, all of the attributes of the row show up except the claim_id.

There is no claim_id, so if a user clicks an edit button, there is no claim_id to tell it where to go.

I need it present the $results of the claim_id regardless is there is a match or not. Any ideas or work arounds?

Thanks.

$query   = "SELECT * FROM claiminfo 
                          LEFT JOIN ( claim_pics ) 
                          ON ( claiminfo.claim_id = claim_pics.claim_id )  
            WHERE( userid='$userid' )   
            ORDER BY claiminfo.ts
            DESC LIMIT 200";

// Will only return

claim_ids that both pics and info match

                  $result = mysql_query( $query );

                  while( $row = mysql_fetch_assoc( $result ) ) { 


                                    $namelocation =      $row['namelocation'];
                                    $claimholder =   $row['claimholder'];
                                    $occasion =      $row['occasion'];
                                    $geotag =    $row['geotag'];
                                    $lat =   $row['lat'] ;
                                    $lng =   $row['lng'];
                                    $claim_id = $row['claim_id']; 
                                    $img = $row['location'];




                                     $html = " ... "
  • 3
    PLEASE DON'T SHOUT. The shift key exists because proper casing makes text easier to read, and SHOUTING is annoying. – Ken White Oct 16 '12 at 01:26
  • 1
    I think this is because the * is picking up the clam_id of claim_pics table instead of your claiminfo table. – You Qi Oct 16 '12 at 01:29
  • My bet is that your limit clause is showing what seems to be only `claiminfo.claim_id = claim_pics.claim_id` - try returning all the rows and see if you get NULLs on the right hand side – Adrian Cornish Oct 16 '12 at 02:14

1 Answers1

0

As comments have noted, it is likely that a null claim_id from the empty claim_pics row is blanking the $row['claim_id']. The easiest way to get around this is to explicitly select and alias the claiminfo.claim_id:

$query   = "SELECT *, claiminfo.claim_id AS id FROM claiminfo 
                          LEFT JOIN ( claim_pics ) 
                          ON ( claiminfo.claim_id = claim_pics.claim_id )  
            WHERE( userid='$userid' )   
            ORDER BY claiminfo.ts
            DESC LIMIT 200";

You can then reliably get the claim_id with the variable $row['id'].

In a more general note, this situation demonstrates why it is generally better not to use SELECT *.

Community
  • 1
  • 1
John C
  • 8,223
  • 2
  • 36
  • 47