Kyle Hudson
Kyle Hudson

Reputation: 898

Unable to retrieve data, mysql php pdo

I have an issue, i cannot get any results from mysql on a production box but can on a development box, we use PHP 5.3 with 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;
}

It just doesnt seem to get into the loopk, i ran the query in navicat and it returns the data.

Really confused :S

Upvotes: 0

Views: 330

Answers (1)

netcoder
netcoder

Reputation: 67745

The following:

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

Will always evaluate to TRUE even if the there are no content in your table. In fact, it will always return a single row named COUNT(*) that contains the number of rows matching your WHERE clause.

You should scrap the first if and do that instead:

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

Upvotes: 1

Related Questions