Page 403 - Beginning PHP 5.3
P. 403
Chapter 12: Introducing Databases and SQL
Summary
Though you can use text files for storing small amounts of data, many Web applications have a need to
store data in databases. In this chapter you were introduced to the concept of databases, and you learned
some basic techniques for working with databases in general, and MySQL in particular:
❑ You studied embedded, client - server, simple, and relational databases, and saw that a relational
database such as MySQL stores its data as rows and columns in tables. You discovered that by
splitting your data over more than one table, you can make your database more efficient — a
process known as normalization
❑ In preparation for working with MySQL, you learned the basics of the SQL language, and
explored MySQL data types and indexes (keys). You also looked at the concept of NULL values
❑ Putting theory into practice, you learned how to start your MySQL server, set up a root
password, create databases and tables, add data to tables, and read, update, and delete data in
tables. You also learned how to delete whole tables and databases
❑ In the final section of the chapter, you took a quick look at connecting to MySQL from your PHP
scripts using the PDO extension. You learned how to set up a connection, how to handle errors,
and how to read data from a table
You build on these skills in the coming chapters, where you start to build some practical database - driven
PHP applications. The next chapter takes a look at how to retrieve MySQL data from within your PHP
scripts. Meanwhile, try the following two exercises to test your knowledge of SQL and of writing
MySQL - enabled PHP scripts. You can find the solution to these exercises in Appendix A.
Exercises
1. Write out an SQL statement that creates a table called members in your mydatabase database to
store information about the members of a book club. Store the following data for each person:
first name, last name, age, and the date they joined the club. Create more SQL statements to
insert five imaginary people into this table:
❑ Jo Scrivener, aged 31, joined September 3, 2006
❑ Marty Pareene, aged 19, joined January 7, 2007
❑ Nick Blakeley, aged 23, joined August 19, 2007
❑ Bill Swan, aged 20, joined June 11, 2007
❑ Jane Field, aged 36, joined March 3, 2006
2. Write a PHP script to query the table you created in Exercise 1, displaying the details of all club
members under 25 years of age.
365
9/21/09 9:11:16 AM
c12.indd 365
c12.indd 365 9/21/09 9:11:16 AM