0

I have some issues with a dynamic query:

    $cond = array();

    if (!empty($type_contrat)) {
        $cond[] = "job_offers.type_contrat = '$type_contrat'";
    }

    if (!empty($categorie_poste)) {
        $cond[] = "job_offers.cat_poste = '$categorie_poste'";
    }

    if (!empty($niveau_etudes)) {
        $cond[] = "job_offers.qualifications = '$niveau_etudes'";
    }

    if (!empty($experience)) {
        $cond[] = "job_offers.experience >= '$experience'";
    }       

    if (count($cond)) {

                $query = $mysqli->query('SELECT 
                job_offers.ref_org,
                job_offers.titre,
                job_offers.qualifications,
                job_offers.experience,
                job_offers.cat_poste,
                job_offers.type_contrat,
                job_offers.taux_occupation,
                job_offers.lieu_affectation,
                job_offers.pays,
                job_offers.url,
                job_offers.date_entered,
                job_offers.date_expire,
                organisations.ref_org,
                organisations.name_organisation
                FROM job_offers,organisations
                WHERE job_offers.ref_org = organisations.ref_org AND ');

                $query .=  implode(' AND ', $cond);
    }


    print_r($query);

--> result: prints only (linebreaks added for readability):

job_offers.type_contrat = '1' AND
job_offers.cat_poste = '3' AND
job_offers.qualifications = '2' AND
job_offers.experience >= '1'

and therefore no result.

1
  • did my answer fix the problem for you? Commented Oct 17, 2014 at 9:59

1 Answer 1

2

You are trying to append a string to a mysqli-result object...

Check the returnvalue of the mysqli->result() function here.

Since someone is keen on removing this answer; here's your solution:

    $cond = array();

    if (!empty($type_contrat)) {
        $cond[] = "job_offers.type_contrat = '$type_contrat'";
    }

    if (!empty($categorie_poste)) {
        $cond[] = "job_offers.cat_poste = '$categorie_poste'";
    }

    if (!empty($niveau_etudes)) {
        $cond[] = "job_offers.qualifications = '$niveau_etudes'";
    }

    if (!empty($experience)) {
        $cond[] = "job_offers.experience >= '$experience'";
    }       

    if (count($cond)) {

                $query = $mysqli->query('SELECT 
                job_offers.ref_org,
                job_offers.titre,
                job_offers.qualifications,
                job_offers.experience,
                job_offers.cat_poste,
                job_offers.type_contrat,
                job_offers.taux_occupation,
                job_offers.lieu_affectation,
                job_offers.pays,
                job_offers.url,
                job_offers.date_entered,
                job_offers.date_expire,
                organisations.ref_org,
                organisations.name_organisation
                FROM job_offers,organisations
                WHERE job_offers.ref_org = organisations.ref_org AND '.implode(' AND ', $cond));
    }


    print_r($query);
Sign up to request clarification or add additional context in comments.

1 Comment

Ah I didn't see he was executing the query before adding to it, you're right.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.