PDO查询不返回行

时间:2015-04-07 02:34:43

标签: php mysql pdo

下面的第一个代码就像一个魅力,而'postclass'表和&我的数据库中的'tpost'表是名称。

$keyword = $_POST['keyword'];
$country = "Thailand";
    if($keyword === "English" || $keyword === "อังกฤษ" || $keyword === "ภาษาอังกฤษ" || $keyword === "english"){
        if($keyword === "English" || $keyword === "english"){
            $sub_alt = "อังกฤษ";
        }
        if($keyword === "อังกฤษ" || $keyword === "ภาษาอังกฤษ"){
            $sub_alt = "English";
        }
    }

    if(isset($sub_alt)){
        $sql="SELECT * FROM `tpost` WHERE `subject` LIKE :search OR `subject` LIKE :search2 AND `country` LIKE :country AND date >= (DATE_SUB(CURDATE(), INTERVAL 150 DAY)) ORDER BY id DESC LIMIT 50";
    } else {
        $sql="SELECT * FROM `tpost` WHERE `country` LIKE :country AND `subject` LIKE :search AND date >= (DATE_SUB(CURDATE(), INTERVAL 150 DAY)) ORDER BY id DESC LIMIT 50";
    }

    $stmt=$dbh->prepare($sql);
    $stmt->bindValue(':search','%'.$keyword.'%', PDO::PARAM_STR);

    if(isset($sub_alt)){
    $stmt->bindValue(':search2','%'.$sub_alt.'%', PDO::PARAM_STR);
    }

    $stmt->bindValue(':country','%'.$country.'%', PDO::PARAM_STR);
    $stmt->execute();


    if ($stmt->rowCount() > 0) {

    $result = $stmt->fetchAll();
    foreach( $result as $row){
    echo "WORKING";
    }

    } else { 
    echo "NO ROWS RETURNED";
    }

可悲的是,下面的这个应该做同样的事情,但它没有返回任何行。数据库中的表设置为InnoDB和UTF-8,结构相同。 $ _POST ['keyword']也发布了正确的数据。使用Jquery $ .post()方法发布。这个脚本中的东西导致BELOW SCRIPT不返回行。在Firefox和Chrome上测试过。有人有想法吗?

$keyword = $_POST['keyword'];
$country = "Thailand";
    if($keyword === "English" || $keyword === "อังกฤษ" || $keyword === "ภาษาอังกฤษ" || $keyword === "english"){
        if($keyword === "English" || $keyword === "english"){
            $sub_alt = "อังกฤษ";
        }
        if($keyword === "อังกฤษ" || $keyword === "ภาษาอังกฤษ"){
            $sub_alt = "English";
        }
    }

    if(isset($sub_alt)){
        $sql="SELECT * FROM `postclass` WHERE `subject` LIKE :search OR `subject` LIKE :search2 AND `country` LIKE :country AND `date` >= (DATE_SUB(CURDATE(), INTERVAL 150 DAY)) ORDER BY `id` DESC LIMIT 50";
    } else {
        $sql="SELECT * FROM `postclass` WHERE `country` LIKE :country AND `subject` LIKE :search AND `date` >= (DATE_SUB(CURDATE(), INTERVAL 150 DAY)) ORDER BY `id` DESC LIMIT 50";
    }

    $stmt=$dbh->prepare($sql);
    $stmt->bindValue(':search','%'.$keyword.'%', PDO::PARAM_STR);

    if(isset($sub_alt)){
    $stmt->bindValue(':search2','%'.$sub_alt.'%', PDO::PARAM_STR);
    }

    $stmt->bindValue(':country','%'.$country.'%', PDO::PARAM_STR);
    $stmt->execute();


    if ($stmt->rowCount() > 0) {

    $result = $stmt->fetchAll();
    foreach($result as $row) {
    echo "WORKING";
    }

    } else { 
    echo "NO ROWS RETURNED";
    }

1 个答案:

答案 0 :(得分:0)

$ dbh-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);

将此添加到我的脚本中以检查错误,完全忘记了我对PHP MYSQL& PDO ...

虽然我必须强调通过将处理程序的attr设置为ERRMODE来检查错误的重要性......

错误表明我的排序规则之间存在冲突,因为在latin1_swedish_ci上设置了一行,转换为utf8_unicode_ci后,行现在在SQL中返回...有时最简单的东西可以让你措手不及...