Introduction to MySQL and using databases with PHP

Sure the idea of dynamic web pages is cool, but you can only go so far when you only use what’s built into PHP, like changing the page based on the day of the week. What you’d really like to do is make a web page unique for each visitor, and that’s where databases come in.

We will begin this chapter assuming that the reader has absolutely no knowledge of MySQL or databases.

What are databases?

Let’s begin our tutorial with our test subject, Sam, who runs a used car dealership. When he first started out, he only had a few cars so keeping track of them was pretty easy. But after while, his dealership began to grow. Soon he had 10 cars on his lot, and a year later he had 25. Every car has number of attributes to track, such make, model, color, year, VIN, number of passengers, body style, MPG, acquisition cost, asking price, etc. As you can imagine, at some point a human just can’t keep track of all that, and even if Sam could keep it all straight, he also needs to convey that information to the sales people.

Finally, Sam would really like to have a web site that allows people to search for the type of car they want, and also to browse the available cars that he has, which of course is always changing. So a static web site isn’t going to be the solution.

Databases help to organize and track things. Databases allow one to use creativity to group things together in meaningful ways, and to present the same set of information in different ways to different audiences.

[box type=”info”] “Databases” are simply an organized collection of data stored in a computer.[/box]

Databases are composed of one or more “tables”. Tables have are composed of parts called “rows” and “columns” similar to what one would see in a spreadsheet. The columns section of each table declares the characteristics of each table while each row contains unique data for each element in the table.

It sounds complicated but is actually quite simple. Take the example below which is one way which Sam could begin to organize his car collection. (Note that not all possible attributes are shown.)

Table: Cars

ID VIN Make Model Style Year Price
1 1328237824 Ford Explorer SUV 2005 5995
2 4797834923 Dodge RAM Pickup 2008 7200
3 2394923724 Mazda 6 Passenger 2010 9995
4 2342323634 Suburu Outback Passenger 2007 4500

 

We can clearly see that the elements in this table have the seven column properties defined as ID, VIN, Make, Model, Style, Year, and Price. The table has four rows that describe four different cars.

Try to think of it as a box holding lots of things organized into smaller and smaller boxes within it.

Here is a quick review of what we have learned.

  • Columns of a table hold the different attributes of each elements in that table. Rows in a table hold different instances uniquely defined by the table’s columns.
  • Tables are just a collection of things that you want to keep track of.
  • Databases are a collection of tables.
Leave a Reply