无法检索数据,mysql php pdo

时间:2010-12-27 22:31:43

标签: php mysql pdo

我有一个问题,我无法从生产盒上的mysql获得任何结果,但可以在开发盒上,我们使用PHP 5.3和MySQL(pdo)。

$sd = $this->dbh->quote($sd);
$si_sql = "SELECT COUNT(*) FROM tbl_wl_data 
           WHERE (site_domain = $sd OR siteDomainMasked = $sd);";
if($this->dbh->query($si_sql)->rowCount() > 0) {
    //gets to here, just doesnt get through the loop
    $sql = "SELECT pk_aid, site_name, site_css, site_img_sw, supportPhone FROM tbl_wl_data
            WHERE (site_domain = $sd OR siteDomainMasked = $sd);";
    foreach($this->dbh->query($sql) as $wlsd) { //-- fails here
        if($wlsd['wl_status'] != '1') {
            require "_domainDisabled.php";
            exit;
        }
        $this->pk_aid = $wlsd['pk_aid'];
        $this->siteTitle = $wlsd['site_name'];
        $this->siteCSS = $wlsd['site_css'];
        $this->siteImage = $wlsd['site_img_sw'];
        $this->siteSupportPhone = $wlsd['supportPhone'];
    }
} else {
    throw new ERR_SITE_NOT_LINKED;
}

它似乎没有进入loopk,我在navicat中运行查询并返回数据。

真的很困惑:S

1 个答案:

答案 0 :(得分:1)

以下内容:

$si_sql = "SELECT COUNT(*) FROM tbl_wl_data WHERE (site_domain = $sd OR siteDomainMasked = $sd);";
if ($this->dbh->query($si_sql)->rowCount() > 0) ...

即使表格中没有内容,也会始终评估为TRUE。实际上,它将始终返回名为COUNT(*)的单行,其中包含与WHERE子句匹配的行数。

您应该废弃第一个if并改为:

$si_sql = "SELECT pk_aid, site_name, site_css, site_img_sw, supportPhone FROM tbl_wl_data WHERE (site_domain = $sd OR siteDomainMasked = $sd);";
if ($this->dbh->query($si_sql)->rowCount() > 0) {
    // foreach here
}