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 />

557<br />

A SQL query that is syntactically valid <strong>and</strong> refers only to databases, tables, <strong>and</strong><br />

columns that exist generally does not fail.The query might, however, return no results if<br />

it is querying an empty table or searching for data that does not exist. Assuming that you<br />

have connected to a database successfully <strong>and</strong> have a table called t1 <strong>and</strong> a column called<br />

c1, the query<br />

select * from t1 where c1 = ‘not in database’;<br />

will succeed but not return any results.<br />

Before you use the result of the query, you need to check for both failure <strong>and</strong> no<br />

results.<br />

Connections to Network Services<br />

Although devices <strong>and</strong> other programs on your system will occasionally fail, they should<br />

fail rarely unless they are of poor quality.When using a network to connect to other<br />

machines <strong>and</strong> the software on those machines, you need to accept that some part of the<br />

system will fail often.To connect from one machine to another, you rely on numerous<br />

devices <strong>and</strong> services that are not under your control.<br />

At the risk of our being repetitive, you really need to carefully check the return value<br />

of functions that attempt to interact with a network service.<br />

A function call such as<br />

$sp = fsockopen('localhost'’, 5000 );<br />

will provide a warning if it fails in its attempt to connect to port 5000 on the machine<br />

localhost, but it will display it in the default format <strong>and</strong> not give your script the option<br />

to h<strong>and</strong>le it gracefully.<br />

Rewriting the call as<br />

$sp = @fsockopen ('localhost', 5000, &$errorno, &$errorstr );<br />

if(!$sp) {<br />

echo "ERROR: ".$errorno.": ".$errorstr;<br />

}<br />

will suppress the built-in error message, check the return value to see whether an error<br />

occurred, <strong>and</strong> use your own code to h<strong>and</strong>le the error message. As the code is written, it<br />

will display an error message that might help you solve the problem. In this case, it<br />

would produce the following output:<br />

ERROR: 10035: A non-blocking socket operation could not be completed immediately.<br />

Runtime errors are harder to eliminate than syntax errors because the parser cannot signal<br />

the error the first time the code is executed. Because runtime errors occur in<br />

response to a combination of events, they can be hard to detect <strong>and</strong> solve.The parser<br />

cannot automatically tell you that a particular line will generate an error.Your testing<br />

needs to provide one of the situations that create the error.

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

Saved successfully!

Ooh no, something went wrong!