1

Please guys Help me on the syntax of my code

<?php
    $detail = mysql_query("SELECT O.geoLocation, O.vehicleId, O.date, OL.oCodeDescription, V.vMake, V.vModel, V.colour
                            FROM offense O, offenselist OL, vehicle V 
                            WHERE OL.oCode = O.offenseListId AND O.offenderNatId =  '$id' AND V.vehicleReg = O.vehicleId");

    while($db_field = mysql_fetch_assoc($detail)){
        $geo = $db_field['O.geoLocation'];
        $vid = $db_field['O.vehicleId'];
        $date = $db_field['O.date'];
        $offense = $db_field['OL.oCodeDescription'];                                                    
        $make = $db_field['V.vMake'];
        $model = $db_field['V.vModel'];
        $colour = $db_field['V.colour'];
        echo"   <tr><td>Date:</td><td>$date</td></tr>
                <tr><td>Offense</td><td>$offense</td></tr>
                <tr><td>Scene Location:</td><td>$geo</td></tr>
                <tr><td>Vehicle Registration No.:</td><td>$vid</td></tr>                                                        
                <tr><td>Vehicle Description:</td><td>$colour $make $model</td></tr>
                <tr><td colspan='2'>&nbsp;</td></tr>
                <tr><td colspan='2'>&nbsp;</td></tr>";

    }
?>

Its returning one of this error among others:

Undefined index: O.geoLocation in C:\wamp\www\eroad\view.php on line 142

5
  • 1
    I think you just have to enter $db_field['geoLocation']; instead of $db_field['O.geoLocation'];
    – ckpepper02
    Apr 23, 2013 at 20:23
  • var_dump($db_field) in order to see the rows in the array
    – Royal Bg
    Apr 23, 2013 at 20:24
  • Unless you have a very good reason, do not use mysql_query in new applications. This interface is deprecated, dangerous, and is being completely removed in future versions of PHP. Learning PDO takes only half an hour and will make your applications future-proof and considerably safer if used correctly.
    – tadman
    Apr 23, 2013 at 20:34
  • Just for formality's sake, I'll post as an answer.
    – ckpepper02
    Apr 23, 2013 at 20:39
  • ok @tadman will do that, thanx also Apr 23, 2013 at 20:39

2 Answers 2

0

I think you just have to enter $db_field['geoLocation']; instead of $db_field['O.geoLocation'];

0

You need to join on an ID that is in common between the two tables, so something like:

$detail = mysql_query(
    "SELECT O.geoLocation, O.vehicleId, O.date, OL.oCodeDescription, V.vMake, V.vModel, V.colour
    FROM offense O 
    INNER JOIN offenselist OL 
        ON O.offenseListId = OL.oCode
    INNER JOIN vehicle V 
        ON O.vehicleId = V.vehicleReg
    WHERE O.offenderNatId = '$id'" 
);

should do the trick.

1
  • Some escaping on $id would make this infinitely safer.
    – tadman
    Apr 23, 2013 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.