Page 387 - Beginning PHP 5.3
P. 387
Chapter 12: Introducing Databases and SQL
Understanding the NULL Value
As well as the data types previously mentioned, MySQL can deal with another special value known
as NULL .
In a MySQL table, a NULL value for a field represents missing data in that field. NULL doesn ’ t belong to
any particular data type, but it can replace any value. Because it is not a data type, even though it can be
in a field, the concept of a NULL is often difficult to grasp. For example, a common mistake is to think of
NULL as zero, which is wrong because zero is a value; NULL is not. Strings filled with one or more blank
spaces, and strings of zero length, may also be mistaken for NULL . NULL is nothing, no data type, no data.
So what happens if the result set from one of your queries contains a NULL , and that result set is then
used in your PHP script in subsequent calculations? Generally speaking, NULL propagates throughout
the calculations. Any arithmetic operation involving a NULL returns NULL . After all, how could you
provide results when all the data needed to perform the calculation are not present?
Don ’ t worry if the concept of NULL seems a bit strange at first. It ’ ll make sense once you start using it in
earnest.
Now let ’ s put all this theory into practice and have some fun with MySQL!
Setting Up MySQL
The MySQL database system comes with a number of different programs. The two important ones that
you learn about here are:
❑ The MySQL server — This is the database engine itself. The program is usually called mysqld or
similar
❑ The MySQL command - line tool — You can use this tool to talk directly to the MySQL server so
that you can create databases and tables, and add, view, and delete data. It ’ s handy for setting
up your databases and also for troubleshooting. The program name is simply mysql
Starting the MySQL Server
If you followed the instructions in Chapter 2 for installing PHP — using Synaptic on Ubuntu,
WampServer on Windows, or MAMP on Mac OS X — then the MySQL server and command - line tool
should already be installed on your computer. In fact, the MySQL server may already be running, but if
it ’ s not, here ’ s how to start it:
❑ Ubuntu — Choose System Administration Services. In the dialog that appears, look for the
“ Database server (mysql) ” item in the list. If there ’ s a check mark to the left of the item, it should
already be running. If not, click Unlock, type your password, and click Authenticate. Now click
the checkbox to the left of the “ Database server (mysql) ” item. The MySQL database server
( mysqld ) should now be running
❑ WampServer on Windows — Examine the WampServer icon in your taskbar. If the icon is black
and white, your Apache and MySQL servers should be running correctly. If the icon is part
yellow or part red, then one or both of the servers aren ’ t running. Click the icon to display the
349
9/21/09 9:11:10 AM
c12.indd 349
c12.indd 349 9/21/09 9:11:10 AM