From the course: Database Foundations: Application Development

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Use if/else to handle errors

Use if/else to handle errors

- [Instructor] The two scripts that we've created so far haven't responded very well if you try and run the code multiple times. To fix this, we can add in some basic error handling to our script using an if-else programming construct. Let me scroll down on my page here until I get to PHP Script 3. In this example, we're going to drop the database that we just created. This time, we're setting up the database connection variable in exactly the same way. We have the variable named db_connection and it's equal to the pg_connect function with these parameters. The only difference here is that we're connecting back to the Postgres database again, not the test database. If we connected to the test database, then we wouldn't be able to drop it, since we would have an open connection. I'm also going to set up the sql_query variable again and this time, the syntax of my sql_query will be DROP DATABASE test WITH FORCE.…

Contents