13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Programming Errors<br />

555<br />

That same statement within the following script is equally wrong:<br />

<br />

Except in the possibly rare case in which the variable $var has the value 4, the call to<br />

strstr() will not occur, <strong>and</strong> no warning will be issued.The <strong>PHP</strong> interpreter does not<br />

waste time parsing sections of your code that are not needed for the current execution<br />

of the script.You need to be sure that you test carefully!<br />

Calling functions incorrectly is easy to do, but because the resulting error messages<br />

identify the exact line <strong>and</strong> function call that are causing the problem, they are equally<br />

easy to fix.They are difficult to find only if your testing process is poor <strong>and</strong> does not test<br />

all conditionally executed code.When you test, one of the goals is to execute every line<br />

of code at least once. Another goal is to test all the boundary conditions <strong>and</strong> classes of<br />

input.<br />

Reading or Writing Files<br />

Although anything can go wrong at some point during your program’s useful life, some<br />

problems are more likely than others. Because errors accessing files are likely enough to<br />

occur, you need to h<strong>and</strong>le them gracefully. Hard drives fail or fill up, <strong>and</strong> human error<br />

results in directory permissions changing.<br />

Functions such as fopen() that are likely to fail occasionally generally have a return<br />

value to signal that an error occurred. For fopen(), a return value of false indicates<br />

failure.<br />

For functions that provide failure notification, you need to carefully check the return<br />

value of every call <strong>and</strong> act on failures.<br />

Interaction with <strong>MySQL</strong> or Other Databases<br />

Connecting to <strong>and</strong> using <strong>MySQL</strong> can generate many errors.The function mysqli_<br />

connect() alone can generate at least the following errors:<br />

n<br />

n<br />

n<br />

Warning: mysqli_connect() [function.mysqli-connect]: Can’t connect<br />

to <strong>MySQL</strong> server on ‘localhost’ (10061)<br />

Warning: mysqli_connect() [function.mysqli-connect]: Unknown <strong>MySQL</strong><br />

Server Host ‘hostname’ (11001)<br />

Warning: mysqli_connect() [function.mysqli-connect]: Access denied<br />

for user: ‘username’@’localhost’ (Using password: YES)<br />

As you would probably expect, mysqli_connect() provides a return value of false<br />

when an error occurs.This means that you can easily trap <strong>and</strong> h<strong>and</strong>le these types of<br />

common errors.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!