I'm really confused here.
This query works perfectly fine on localhost, but not on any actual live server. Other queries on the same database work, so there's nothing wrong with the connection or with the pdo extension.
Basically this is the query:
$sql = "SELECT * FROM bksb_Resources";
$query = $this->connection->query($sql, array());
The query() method returns the PDOStatement object
Then I call $this->connection->fetch($query) in a loop, which in turn gets routed to a method on the "connection" object:
function fetch_pdo($st)
{
return $st->fetch();
}
So basically it's selecting all from a table and then returning the first row (it's a loop but that's not really relevant).
On localhost, this is fine. No problems.
On my live server i get:
Fatal error: Cannot access empty property in /var/www/html/demo/blocks/elbp/classes/db/MSSQL.class.php on line 652
Where line 652 is the return from the fetch_pdo method above.
I've printed out the statement before it runs the fetch() on it and it is fine on both servers:
PDOStatement Object
(
[queryString] => SELECT * FROM bksb_Resources
)
I'm out of ideas really. I can't see why this would work on one server and not another, when the rest of the queries work fine on the live server as well.
(Well... I say the rest, this was originally a different problem where the query on localhost returned the correct results and the query on live returned nothing, even though if i ran the copy/pasted query on Management Studio it worked fine... but in trying to debug that I've come across this one).
Anyone got any ideas?
Thanks
Edit:
The call to $this->connection->query($sql, array());
Calls this:
/**
* Run an SQL query and return a statement - to be used for things like selecting
* @param type $sql
* @param type $params
* @return type
*/
public function query($sql, $params){
$this->lastSQL = $sql;
$func = 'query_'.$this->extension;
return $this->$func($sql, $params);
}
Which then calls this:
/**
* Run SQL query using PDO
* @param type $sql
* @param type $params
* @return $st Statement
*/
private function query_pdo($sql, $params)
{
$st = $this->dbh->prepare($sql);
$st->execute($params);
return $st;
}
And if you want it as well, this is the loop where I'm calling the fetch:
$results = array();
while($row = $this->connection->fetch($query)){
$results[] = $row;
}
I recently encountered this error with PDO and a MySQL backend. This was due to the fact that my query was returning a column with an empty alias, like the following query :
SELECT name as "" FROM users;
It is a perfectly valid query (in the sense that MySQL will interpret and execute it perfectly) but this will lead to a "Cannot access empty property" fatal error when PDO tries to fetch the data and FetchMode is set to PDO::FETCH_OBJ.
This should give anyone with the same problem some hint on how to fix it. At some point, PDO will instantiate an stdClass and attempt to set a property for each column returned by the query. When a column with an empty name is returned, PDO will basically execute the following code :
// $obj is an stdClass instance
$prop = ''; // the name of the query column
$obj->$prop = $value; // the value of the query columns
This leads to the "Cannot access empty property" fatal error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With