7

How do I know if there's an error if I did $db = new SQLite3("somedb.db"); in PHP? Right now the $db doesn't really give me any sort of error?

I can check for file existance, but I'm unsure if there could be any other errors when I open a connection.

2 Answers 2

7

Try:

echo $db->lastErrorMsg();
Sign up to request clarification or add additional context in comments.

Comments

7

You should enable exceptions and instantiate in a try-catch block.

It is not obvious from the documentation but if you use the constructor to open the database it will throw an exception on error.

Further if you set the flag SQLITE3_OPEN_READWRITE in the second argument then it will also throw an exception when the database does not exist (rather than creating it).

class Database extends SQLite3
{
    function __construct($dbName)
    {
        $this->enableExceptions(true);

        try
        {
            parent::__construct($dbName, SQLITE3_OPEN_READWRITE );
        }
        catch(Exception $ex) { die( $ex->getMessage() ); }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.