Specifications

empty table or searching for data that does not exist. Assuming that you have connected to a
database successfully, and have a table called exists and a column called column name, the
following query, for example
select * from exists where column_name = ‘not in database’;
will succeed but not return any results.
Before you use the result of the query, you will need to check for both failure and no results.
Connections to Network Services
Although devices and other programs on your system will occasionally fail, they should fail
rarely unless they are of poor quality. When using a network to connect to other machines and
the software on those machines, you will need to accept that some part of the system will fail
often. To connect from one machine to another, you are relying on numerous devices and ser-
vices that are not under your control.
At the risk of being repetitive, you really need to carefully check the return value of functions
that attempt to interact with a network service.
A function call such as
$sp = fsockopen ( “localhost”, 5000 );
will not provide an error message if it fails in its attempt to connect to port 5000 on the
machine localhost.
Rewriting the call as
$sp = fsockopen ( “localhost”, 5000, &$errorno, &$errorstr );
if(!$sp)
echo “ERROR: $errorno: $errorstr”;
will check the return value to see if an error occurred, and display an error message that might
help you solve the problem. In this case, it would produce the output:
ERROR: 111: Connection refused
Runtime errors are harder to eliminate than syntax errors because the parser cannot signal the
error the first time the code is executed. Because runtime errors occur in response to a combi-
nation of events, they can be hard to detect and solve. The parser cannot automatically tell you
that a particular line will generate an error. Your testing needs to provide one of the situations
that create the error.
Handling runtime errors requires a certain amount of forethought; to check for different types
of failure that might occur, and then take appropriate action. It also takes careful testing to sim-
ulate each class of runtime error that might occur.
Building Practical PHP and MySQL Projects
P
ART V
484
29 7842 CH23 3/6/01 3:41 PM Page 484