Check if database and/or table exists?

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; }.

Thank you, I was hoping there were some Zend-specific functions for that, but this works OK, so I'll settle with this. Just a remark: catch needs parentheses (catch(Exception $e) {...) :). – mingos Feb 22 at 13:30 Yeah, thanks, I'll edit it.

Just did a fast copy/past thingy :p – Shikiryu Feb 22 at 13:32 just as a side note, you can use the IF (NOT) EXISTS functions of SQl to verify if a database/table exists. – Simon Feb 22 at 13:58 @Simon : You're right and, even if an answer is accepted, you should put what you think could help the OP (or future comers) as an answer. But, here, using that kind of PDO won't require such query since it's usually already the cause of the exception thrown.

– Shikiryu Feb 22 at 14:05 @Simon: I tried these first, but with Zend Framework, they just didn't do the trick :(. – mingos Feb 22 at 15:27.

Test if the database specified in application. Ini exists (or if there's access to it). Should any of the steps fail, the controller will take care of redirecting the user to the proper step of the installation process.

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?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions