I am trying to write a simple installation script using Zend Framework. It's supposed to run a few tests:
user exists within the databaseShould any of the steps fail, the controller will take care of redirecting the user to the proper step of the installation process.
I created a model with the following code:
public function verify () {
$db = $this->getDefaultAdapter(); //throws exception
if ($db == null) return self::NO_BATABASE;
$result = $db->describeTable('user'); //throws exception
if (empty($result)) return self::NO_USER;
$result = $db->fetchRow('SELECT * FROM user WHERE id = 1');
if ($result == null) return self::USER_EMPTY;
else return self::OK;
}
However, I overestimated the functions I have used. getDefaultAdapter() may return null, but in case there's no database to connect to, an exception is thrown. Same happens with describeTable(), which throws an exception instead of returning an empty array.
My question is therefore: how do I check whether the database/table exists without getting an exception or error?
Rough example :
public function verify () {
try{
$db = $this->getDefaultAdapter(); //throws exception
if ($db == null) return self::NO_BATABASE;
}catch(Exception $e){
return self::NO_BATABASE;
}
try{
$result = $db->describeTable('user'); //throws exception
if (empty($result)) return self::NO_USER;
}catch(Exception $e){
return self::NO_USER;
}
$result = $db->fetchRow('SELECT * FROM user WHERE id = 1');
if ($result == null) return self::USER_EMPTY;
else return self::OK;
}
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