Check if database and / or table exists

I am trying to write a simple installation script using the Zend Framework. It should do some tests:

  • check if the database specified in the application.ini application (or access to it) exists.
  • if so, check if user table exists in the database
  • if so, check if there is an admin site user

If any of the steps fails, the controller will take care of redirecting the user to the correct step in 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 features that I used. getDefaultAdapter() may return null , but if there is no database to connect, an exception is thrown. The same thing happens with describeTable() , which throws an exception instead of returning an empty array.

So my question is: how can I check if a database / table exists without getting an exception or error?

+7
source share
1 answer

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; } 
+10
source

All Articles