0

The main table is CAR table that looks like this: enter image description here

The next table is a MODEL table that looks like

enter image description here

The other table that I'm dealing with is called BODY_TYPE and it looks like this

enter image description here

The last table that I'm dealing with is called MAKE and it looks as illustrated in the following image

enter image description here

Now what I'm looking to achieve is to fetch MODEL that matches BODY_TYPE and the MAKE and must be available on the CAR table.

Basically, CAR contains all cars, so the data that I'm supposed to fetch must be in CAR. If a model is not available on the car table then it should not be shown.

this is my code but it keeps returning empty results. Not sure where am going wrong. I have tried for hours with no success

function fetch_model_options( $boody_type, $make ){
    $conbd = Conectar_two();

    $Query = "select mo.model from CAR c, MODEL mo, MAKE m , BODY_TYPE bo
            WHERE mo.id_model = c.id_model and 
            c.id_make = m.id_make AND 
            (
                ( mo.id_body_type = bo.id_body_type AND bo.body_type LIKE '%$boody_type%' AND c.id_body_type = bo.id_body_type  )
                AND
                ( mo.id_make = m.id_make AND m.make LIKE '%$make%' )
            )
            AND
            c.available =1 
            group by m.id_make";

    
    if ($Result = $conbd->query($Query)){
        echo "There are some results\n";

        while($reg = $Result->fetch_array(MYSQLI_BOTH)){

            
        echo '<option value="'.$reg['model']. '">'.$reg['model']. '</option>' . "\n";                                       
        //printf("<select class='form-control'>" ,$reg["body_type"]);   
        //$tmp = "<select class='form-control' id='".$reg["body_type"]."'>";
        
        //echo("<option value='' id='".$reg["body_type"]."'>");
        //echo $tmp;
        //printf($reg["body_type"]); 
        
        }
        $Result->free();
    } else {
        echo "no results\n";

    }

//$conbd->free();
//<option value="">All</option>

}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Karue Benson Karue
  • 957
  • 12
  • 26
  • 2
    Please learn to use ANSI JOIN in your queries, not cross products. They're much easier to read and write. – Barmar Dec 17 '21 at 00:00
  • You're missing `AND` before `c.available =1` – Barmar Dec 17 '21 at 00:00
  • 1
    You need to add error checking, then you would have know about the syntax error. – Barmar Dec 17 '21 at 00:02
  • I have added the missing AND and edited the question. My question is however not in anyway related to the pointed duplicate questioni – Karue Benson Karue Dec 17 '21 at 00:19
  • You have ` – Barmar Dec 17 '21 at 00:22
  • Which message are you getting, "there are some results" or "no results"? – Barmar Dec 17 '21 at 00:24
  • for now i"m using console I'm expecting to see the right results. forget about the HTML used to render data. Essentially think of it like just a normal echo without HTML for that matter – Karue Benson Karue Dec 17 '21 at 00:25
  • If the PHP is not relevant, you should leave it out of the question so it doesn't confuse the issue. Can you make a fiddle with sample data? We can't cut and paste your screen shots. – Barmar Dec 17 '21 at 00:31
  • Why are you using `GROUP BY m.id_make` without any aggregation functions? – Barmar Dec 17 '21 at 00:33
  • FYI, your "no results" message is printed when there's an error in the query, not when the query is valid but doesn't find any matches. – Barmar Dec 17 '21 at 01:00

0 Answers0