This code works perfectly on it’s own or in a snippet:
$result = $modx->query("SELECT * from `modx_site_content` WHERE (id = '1' ");
if ($result === false) {
$modx->log(modX::LOG_LEVEL_ERROR, 'Result is false');
}
if (! is_object($result)) {
$modx->log(modX::LOG_LEVEL_ERROR, 'Result is not an object');
}
The $result is not an object and is false.
In my CMP, however, neither case is triggered. The call to $modx->query() appears to return an object containing the query string and nothing else when there are no matches.
IIRC, it’s only false if the query is invalid and can’t be prepared. Perhaps when the DB server can’t be reached as well.
In other cases, where the query is valid but simply doesn’t have any results, you’re still going to get an iterable PDOStatement object or array if you were to provide the fetch mode - it’s just going to be empty.
Perhaps checking empty($result) is what you’re looking for?
I should have mentioned that I tried that. It’s not empty, but I did find a workaround since iterating through it produces no results. I just moved the test further down and used if (empty($output)).
Thanks everyone for the help. The docs here are misleading. This example doesn’t work:
$result = $modx->query("SELECT * FROM modx_users WHERE id=1");
if (!is_object($result)) {
return 'No result!';
}
else {
$row = $result->fetch(PDO::FETCH_ASSOC);
return 'Result:' .print_r($row,true);
}