Presentation is loading. Please wait.

Presentation is loading. Please wait.

2010/11 : [1]Building Web Applications using MySQL and PHP (W1)Application Error Handling.

Similar presentations


Presentation on theme: "2010/11 : [1]Building Web Applications using MySQL and PHP (W1)Application Error Handling."— Presentation transcript:

1 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)Application Error Handling

2 2010/11 : [2]Building Web Applications using MySQL and PHP (W1)Application Error Handling ‘Application’..? An ‘application’ in this case is any user interactive interface powered by PHP and MySQL. In this course, we concentrate on XHTML web interfaces. Although you have the technical knowledge of PHP and MySQL, there are a number of issues in a web environment that we haven’t discussed.

3 2010/11 : [3]Building Web Applications using MySQL and PHP (W1)Application Error Handling Failure is Inevitable. The world is a fallible place. Networks fail. Databases crash. Web servers get misconfigured. Robust applications are built with failure in mind. These slides are about handling web application failure when using PHP & MySQL.

4 2010/11 : [4]Building Web Applications using MySQL and PHP (W1)Application Error Handling Types of Error Handling On a desktop application, complicated error handling logic is required to prevent corruption of the application ‘state’ by unexpected failure. Such error handling can be undertaken by a web app too. However.. Web apps don’t persist much application state, so ‘Get the Hell outta there!’ error handling is often preferred.

5 2010/11 : [5]Building Web Applications using MySQL and PHP (W1)Application Error Handling Get the Hell Outta There! Simply exit the application (apologising to the the user) if an error occurs. $link = mysqli_connect(…); /* exit if can’t contact database */ if (mysqli_connect_errno()) { exit(‘Please try again later.’); }

6 2010/11 : [6]Building Web Applications using MySQL and PHP (W1)Application Error Handling Failure Points..? Some obvious failure points can be identified, usually associated with external resources: database connection, query failure. What is less obvious is that some sort of hardware fault/power problem might stop your script processing at any mid-way point.

7 2010/11 : [7]Building Web Applications using MySQL and PHP (W1)Application Error Handling Protect the Database. Your database is your one persistent store of information. Protect it at all costs. You need to make sure that any failure cannot corrupt the data in your database. The sort of corruption that can occur is best illustrated by an example.

8 2010/11 : [8]Building Web Applications using MySQL and PHP (W1)Application Error Handling Bank Transfer Example Si owes Rob £100, so transfers it across.. -- get si's balance SELECT balance FROM accounts WHERE name='si'; -- check its > 100, if so -- continue by deducting -- amount from si's account UPDATE accounts SET balance = balance-100 WHERE name='Si'; -- credit the money to rob UPDATE accounts SET balance = balance+100 WHERE name='Rob'; SELECT * FROM accounts; +------+---------+ | name | balance | +------+---------+ | rob | 1456.17 | | si | 2715.45 | +------+---------+

9 2010/11 : [9]Building Web Applications using MySQL and PHP (W1)Application Error Handling Corruption? Say for some reason the last query (to credit Rob’s account) failed.. No money would have reached Rob’s account, but the money would still have been deducted from Si’s account! Money has disappeared. Ooops. In other words, a failure has corrupted our database.

10 2010/11 : [10]Building Web Applications using MySQL and PHP (W1)Application Error Handling Transactions Often a sequence of queries need to be executed in a particular order, and if one query fails the whole lot fails. In other words, the queries need to be grouped. The classic example is that of a bank transaction.

11 2010/11 : [11]Building Web Applications using MySQL and PHP (W1)Application Error Handling MySQL Transaction Syntax Start transaction group: START TRANSACTION; Finish transaction and ‘save’ changes: COMMIT; Finish transaction and ‘undo’ everything: ROLLBACK;

12 2010/11 : [12]Building Web Applications using MySQL and PHP (W1)Application Error Handling Transactions and Table Type MySQL actually has a number of different table types. (http://dev.mysql.com/doc/refman/5.1/en/storage-engines.html) (http://dev.mysql.com/doc/refman/5.1/en/storage-engines.html) To use transactions, we need to use the InnoDB table type (it is not the default). CREATE TABLE accounts ( name VARCHAR(255), balance DECIMAL(10,2) ) ENGINE=InnoDB;

13 2010/11 : [13]Building Web Applications using MySQL and PHP (W1)Application Error Handling Transaction Example Back to the transfer of money.. we now enclose the queries in a transaction. If a query fails, we can explicitly undo everything ourselves using ROLLBACK. START TRANSACTION; SELECT balance FROM accounts WHERE name='si'; UPDATE accounts SET balance = balance-100 WHERE name='Si'; UPDATE accounts SET balance = balance+100 WHERE name='Rob'; COMMIT; or ROLLBACK;

14 2010/11 : [14]Building Web Applications using MySQL and PHP (W1)Application Error Handling Transactions with mysqli Start transaction group: mysqli_query($link,‘START TRANSACTION’); Finish transaction and ‘save’ changes: mysqli_commit($link); Finish transaction and ‘undo’ everything: mysqli_rollback($link);

15 2010/11 : [15]Building Web Applications using MySQL and PHP (W1)Application Error Handling Exercise Hands On Exercise: Application Error Handling

16 2010/11 : [16]Building Web Applications using MySQL and PHP (W1)Application Error Handling Review Introduced the awareness that failures need to be considered. Within PHP, the best option for serious errors is often a ‘give up and quit’ approach. The most important consideration is protecting the database. Transactions can be used to achieve this.


Download ppt "2010/11 : [1]Building Web Applications using MySQL and PHP (W1)Application Error Handling."

Similar presentations


Ads by Google