PHP SQL - Sort by country

325 views Asked by At

So, I've been having some issues with a school project I'm working on. I have a list of participants of a cooking contest. There's 4 from 5 different countires, and I want to be able to sort the contestants by their countries or show all, if no country is selected.

I have used if/else statements using a $_GET with limited success.

if(isset($_GET['country'])){
     $sqlCountry = "SELECT * FROM countries WHERE country_name = ".$_GET['country'];
}else{
     $sqlCountry = "SELECT * FROM countires";
}
$queryCountry = mysqli_query($db,$sqlCountry );
$count = 1;

while($resCountry=mysqli_fetch_array($queryCountry )){
    if(isset($_GET['land'])){
        $country= $_GET['country'];
    }else{
        $country= $resCountry['country_navn'];
    }

$sql_Cont = "SELECT * FROM contestants WHERE cont_country='$country'";
$query_Cont = mysqli_query($db,$sql_Cont );

echo    '<div class="mod12 mod_gen no-margin">
            <h2>'.$resCountry['land_navn'].'</h2>';
            while($res_Cont=mysqli_fetch_array($query_Cont )){
                $count++;
                if($count % 2){
                    $right = '';
                }else{
                    $right = 'right';
                }
                echo    '<div class="mod6 mod_gen '.$right.'">
                            <div class="venstre">
                                <img src="images/placeholder.jpg">
                            </div>
                            <div class="hoejre">
                                <h3>'.$res_Cont['cont_name'].'</h3>
                                <h5>'.$res_Cont['cont_workplace'].' - '.$res_Cont['del_home_city'].'</h5>
                                <br>
                                <p>Speciality: '.$res_Cont['cont_speciality'].'<br><br>
                                '.nl2br($res_Cont['cont_text']).'</p>
                            </div>
                        </div>';
            }
echo    '</div>';

Everything works up to the point where I click on one of the links to get the country name up in the URL: index.php?page=2&country=Denmark for instance. After that I'll just give me a fetch_array expects parameter to be 1, Boolean given error.

In my mind, this should work.

Any help appriciated. Best regards ~Barakna

1

There are 1 answers

6
Saty On BEST ANSWER

As per your question your error is

Everything works up...fetch_array expects parameter to be 1,

You have to change your query . Instead of your query

$sqlCountry = "SELECT * FROM countries WHERE country_name = ".$_GET['country'];

Your query variable in on quotes and table name and field name in backtick . Also use mysqli_real_escape_string to prevent sql injection

$country = mysqli_real_escape_string($db, $_GET['country']);

$sqlCountry = "SELECT * FROM `countries` WHERE `country_name` ='".$country."'";