简体   繁体   English

PDO SQL查询无法正常工作

[英]PDO SQL Query not working as intended

I have this function in PHP: 我在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;
}

If i pass any parameters for the job_app it just returns 'Sorry, there doesn't seem to be any results!' 如果我为job_app传递了任何参数,它只会返回“对不起,似乎没有任何结果!” Although there is results because some of the rows in fact have the job-app parameter in them. 尽管有结果,因为实际上某些行中包含job-app参数。 If i pass job_app as 2 and job_type as 2 it should come up with 6 results however it doesn't. 如果我将job_app设置为2并将job_type设置为2,则应该给出6个结果,但事实并非如此。 Other parts of Relevent PHP: Relevent PHP的其他部分:

DB List function: 数据库列表功能:

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 file that calls function: 调用函数的.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);
}
}

Am i missing something to filter them properly? 我是否缺少适当过滤的东西? I have read through it myself a few times and it seems right but just doesn't filter as intended. 我本人已经阅读了几次,这似乎是正确的,但是没有按预期进行过滤。

EDIT: 编辑:

I didn't seem to make it clear enough, it does succeed with the query as it returns the end else statement in the while loop. 我似乎不太清楚,它在查询中成功,因为它在while循环中返回end else语句。

You generate your error message in the while loop so you are definitely getting results: 您在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>';
    }
}

Your problem is that the if conditions don't match in any of the rows. 您的问题是if条件在任何行中都不匹配。 You need to figure out why, for example by dumping the rows. 您需要找出原因,例如通过转储行。

I changed the function so that the job_app was filtered in the query rather than in a IF statement later on 我更改了函数,以便稍后在查询中而不是在IF语句中过滤job_app

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM