PDO SQL查询无法按预期工作

时间:2016-03-04 10:15:05

标签: php mysql pdo

我在PHP中有这个功能:

function getFilteredList($conn, $limit, $offset, $job_app, $Job_Type, $location) {
    /*
    <option value="0">Select category or Search all</option>
    <option value="1">Business and Administration</option>
    <option value="2">Education and Training</option>
    <option value="3">Leisure, Travel and Tourism</option>
    <option value="4">Information and Communication Technology</option>
    */
    if($Job_Type == 1){
        $Job_Type2 = '%1%';
    }elseif($Job_Type == 2){
        $Job_Type2 = '%2%';
    }elseif($Job_Type == 3){
        $Job_Type2 = '%3%';
    }elseif($Job_Type == 4){
        $Job_Type2 = '%4%';
    }
    $query = $conn->prepare("SELECT * FROM `jobs_current` WHERE `job_type` LIKE :job_type ORDER BY `jobs_current`.`job_comp_loc` ASC LIMIT :limit OFFSET :offset ");
    $query->bindParam(':limit', $limit, PDO::PARAM_INT);
    $query->bindParam(':offset', $offset, PDO::PARAM_INT);
    $query->bindParam(':job_type', $Job_Type2, PDO::PARAM_STR);
    $query->execute();

    $end = "";
    while ($row = $query->fetch(PDO::FETCH_ASSOC))
    {
        if($row['job_app'] == $job_app){
            $end = $end.'<tr class="hoverOver"><td class="td1"><a   href="/jobview.php?id='.$row['id'].'">'.$row['job_comp_loc'].'</a></td>';
            $end = $end.'<td class="td2"><a   href="/jobview.php?id='.$row['id'].'">'.$row['job_desc'].'</a></td>';
            $end = $end.'<td class="td3"><a   href="/jobview.php?id='.$row['id'].'">'.$row['location'].'</a></td></tr>';
        }else if($job_app == 0){
            $end = $end.'<tr class="hoverOver"><td class="td1"><a   href="/jobview.php?id='.$row['id'].'">'.$row['job_comp_loc'].'</a></td>';
            $end = $end.'<td class="td2"><a   href="/jobview.php?id='.$row['id'].'">'.$row['job_desc'].'</a></td>';
            $end = $end.'<td class="td3"><a   href="/jobview.php?id='.$row['id'].'">'.$row['location'].'</a></td></tr>';
        }else{
            return '<td class="td1"></td><td class="td2" style="text-align: center;color: red;">Sorry, there doesnt seem to be any results!</td><td class="td3"></td>';
        }

    }
    return $end;
}

如果我传递job_app的任何参数,它只会返回'抱歉,似乎没有任何结果!'虽然有结果,因为其中一些行实际上有job-app参数。如果我将job_app传递给2并将job_type传递给2,那么它应该会得到6个结果,但事实并非如此。 Relevent PHP的其他部分:

DB List功能:

function getGenericList($conn, $limit, $offset, $job_app) {

    $query = $conn->prepare("SELECT * FROM `jobs_current` ORDER BY `jobs_current`.`job_comp_loc` ASC LIMIT :limit OFFSET :offset ");
    $query->bindParam(':limit', $limit, PDO::PARAM_INT);
    $query->bindParam(':offset', $offset, PDO::PARAM_INT);
    $query->execute();

    $end = "";
    while ($row = $query->fetch(PDO::FETCH_ASSOC))
    {
        if($row['job_app'] == $job_app){
            $end = $end.'<tr class="hoverOver"><td class="td1"><a   href="/jobview.php?id='.$row['id'].'">'.$row['job_comp_loc'].'</a></td>';
            $end = $end.'<td class="td2"><a   href="/jobview.php?id='.$row['id'].'">'.$row['job_desc'].'</a></td>';
            $end = $end.'<td class="td3"><a   href="/jobview.php?id='.$row['id'].'">'.$row['location'].'</a></td></tr>';
        }else if($job_app == 0){
            $end = $end.'<tr class="hoverOver"><td class="td1"><a   href="/jobview.php?id='.$row['id'].'">'.$row['job_comp_loc'].'</a></td>';
            $end = $end.'<td class="td2"><a   href="/jobview.php?id='.$row['id'].'">'.$row['job_desc'].'</a></td>';
            $end = $end.'<td class="td3"><a   href="/jobview.php?id='.$row['id'].'">'.$row['location'].'</a></td></tr>';
        }else{
            return '<td class="td1"></td><td class="td2" style="text-align: center;color: red;">Sorry, there doesnt seem to be any results!</td><td class="td3"></td>';
        }

    }
    return $end;
}
调用函数的

.php文件:

$Job_Type = null;
$Location = null;
$Job_App = 0;
if(!$_GET['Job_App'] == null){
    $Job_App = $_GET['Job_App'];
}
if(!$_GET['Job_Type'] == null){
    if(!$_GET['Job_Type'] == 0){
        $Job_Type = $_GET['Job_Type'];
    }
}
if(!$_GET['Location'] == null){
    $Location = $_GET['Location'];
}
//irreleven stuff in between
if(!$_GET['page'] == null){
    $pageno = ($_GET['page']-1)*15;
    if(!$_GET['Job_Type'] == null){
        echo ''.getFilteredList($dbh, 15, $pageno, $Job_App, $Job_Type, $Location);
    }else{
        echo ''.getGenericList($dbh, 15, $pageno, $Job_App);
    }
}else{
    if(!$_GET['Job_Type'] == null){
        echo ''.getFilteredList($dbh, 15, 0, $Job_App, $Job_Type, $Location);
}else{
    echo ''.getGenericList($dbh, 15, 0, $Job_App);
}
}

我错过了正确过滤它们的东西吗?我已经自己阅读了几次,看起来是正确的,但只是没有按预期过滤。

编辑:

我似乎没有说清楚,它在查询中成功,因为它在while循环中返回结束else语句。

2 个答案:

答案 0 :(得分:0)

您在while循环中生成错误消息,因此您肯定会得到结果:

while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
    if($row['job_app'] == $job_app){
        $end = $end.'<tr class="hoverOver"><td class="td1"><a   href="/jobview.php?id='.$row['id'].'">'.$row['job_comp_loc'].'</a></td>';
        $end = $end.'<td class="td2"><a   href="/jobview.php?id='.$row['id'].'">'.$row['job_desc'].'</a></td>';
        $end = $end.'<td class="td3"><a   href="/jobview.php?id='.$row['id'].'">'.$row['location'].'</a></td></tr>';
    }else if($job_app == 0){
        $end = $end.'<tr class="hoverOver"><td class="td1"><a   href="/jobview.php?id='.$row['id'].'">'.$row['job_comp_loc'].'</a></td>';
        $end = $end.'<td class="td2"><a   href="/jobview.php?id='.$row['id'].'">'.$row['job_desc'].'</a></td>';
        $end = $end.'<td class="td3"><a   href="/jobview.php?id='.$row['id'].'">'.$row['location'].'</a></td></tr>';
    }else{
        // Here you are in the while loop so you have results!
        return '<td class="td1"></td><td class="td2" style="text-align: center;color: red;">Sorry, there doesnt seem to be any results!</td><td class="td3"></td>';
    }
}

您的问题是if条件在任何行中都不匹配。你需要找出原因,例如通过转储行。

答案 1 :(得分:0)

我更改了函数,以便在查询中过滤job_app,而不是稍后在IF语句中过滤

function getFilteredList($conn, $limit, $offset, $job_app, $Job_Type, $location) {
    /*
    <option value="0">Select category or Search all</option>
    <option value="1">Business and Administration</option>
    <option value="2">Education and Training</option>
    <option value="3">Leisure, Travel and Tourism</option>
    <option value="4">Information and Communication Technology</option>
    */
    if($Job_Type == 1){
        $Job_Type2 = '%1%';
    }elseif($Job_Type == 2){
        $Job_Type2 = '%2%';
    }elseif($Job_Type == 3){
        $Job_Type2 = '%3%';
    }elseif($Job_Type == 4){
        $Job_Type2 = '%4%';
    }else{
        $Job_Type2 = '%1%2%3%4%';
    }
    $query = $conn->prepare("SELECT * FROM `jobs_current` WHERE `job_type` LIKE :job_type AND `job_app` = :job_app ORDER BY `jobs_current`.`job_comp_loc` ASC LIMIT :limit OFFSET :offset ");
    $query->bindParam(':limit', $limit, PDO::PARAM_INT);
    $query->bindParam(':offset', $offset, PDO::PARAM_INT);
    $query->bindParam(':job_app', $job_app, PDO::PARAM_INT);
    $query->bindParam(':job_type', $Job_Type2, PDO::PARAM_STR);
    $query->execute();

    $end = "";
    while ($row = $query->fetch(PDO::FETCH_ASSOC))
    {
        if($row !== null){
            $end = $end.'<tr class="hoverOver"><td class="td1"><a   href="/jobview.php?id='.$row['id'].'">'.$row['job_comp_loc'].'</a></td>';
            $end = $end.'<td class="td2"><a   href="/jobview.php?id='.$row['id'].'">'.$row['job_desc'].'</a></td>';
            $end = $end.'<td class="td3"><a   href="/jobview.php?id='.$row['id'].'">'.$row['location'].'</a></td></tr>';
        }else{
            return '<td class="td1"></td><td class="td2" style="text-align: center;color: red;">Sorry, there doesnt seem to be any results!</td><td class="td3"></td>';
        }

    }
    return $end;
}