Using PHP5 with MySQL
So now that you’ve done some really cool stuff with PHP in Chapter 2, such as using includes and functions, it’s time to make your site truly dynamic and show users some real data. You may or may not have had experience with databases, so we’ll take a look at what MySQL is and how PHP can tap into the data. We will also show you what a MySQL database looks like in terms of the different tables and fields and give you some quickie shortcuts to make your life much easier (you can thank us later for those).
By the end of this chapter, you will be able to:
❑ Understand a MySQL database
❑ View data contained in the MySQL database
❑ Connect to the database from your Web site
❑ Pull specific information out of the database, right from your Web site ❑ Use third-party software to easily manage tables
❑ Use the source Web site to troubleshoot problems you may encounter Although some of this information is expanded upon in later chapters, this chapter lays the groundwork for more complex issues.
Overview of MySQL Structure and Syntax
MySQL is a relational database system, which basically means that it can store bits of information in separate areas and link those areas together. You can store virtually anything in a database: the contents of an address book, a product catalog, or even a wish list of things you want for your birthday.
In the sites you create as you work through this book, you are storing information pertinent to a movie review site (such as movie titles and years of release) and comic book fan information (such as a list of authentic users/comic book fans and their passwords). MySQL commands can be issued through the command prompt, as you did in Chapter 1 when you were installing it and granting permissions to users, or through PHP. We primarily use PHP to issue commands in this book, and we will discuss more about this shortly.
Because MySQL is a relational database management system, it allows you to separate information into tables or areas of pertinent information. In nonrelational database systems, all the information is stored in one big area, which makes it much more difficult and cumbersome to sort and extract only the data you want. In MySQL, each table consists of separate fields, which represent each bit of information. For example, one field could contain a customer’s first name, and another field could contain his last name. Fields can hold different types of data, such as text, numbers, dates, and so on. You create database tables based on what type of information you want to store in them. The separate tables of MySQL are then linked together with some common denominator, where the values of the common field are the same.
For an example of this structure, imagine a table that includes a customer’s name, address, and ID number, and another table that includes the customer’s ID number and past orders he has placed. The common field is the customer’s ID number, and the information stored in the two separate tables would be linked together via fields where the ID number is equal. This enables you to see all the information related to this customer at one time.
Let’s take a look at the ways in which you can tailor database tables to fit your needs.
When you create a table initially, you need to tell MySQL server what types of information will be stored in each field. The different types of fields and some examples are listed in the table that follows.
Although the preceding field types should suffice for most needs, the table that follows lists some perhaps less-often-used types.
Believe it or not, even more data types are supported by MySQL; you can find a comprehensive list of them in Appendix D.
Choosing the Right Field Type
Although you won’t actually be creating a database from scratch just yet, you should know how to figure out what field type will best serve your needs. We’ve put together a list of questions about...
Please join StudyMode to read the full document