动态查询php / mysql

时间:2014-10-13 15:23:09

标签: php sql mysqli

我在动态查询方面遇到了一些问题:

    $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);

- >结果:仅打印(为了便于阅读而添加了换行符):

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

因此没有结果。

1 个答案:

答案 0 :(得分:2)

您正在尝试将字符串附加到mysqli-result对象...

检查mysqli-> result()函数here的返回值。

因为有人热衷于删除这个答案;这是你的解决方案:

    $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);