Introduction
We use databases to store information and the most popular database used with PHP is MySQL. It’s also free, which is very convenient for us! If you’d like to build a site like Facebook, or store your customer’s order history, or keep an inventory of car parts, then you will need to use a database.
What is MySQL?
MySQL is an open-source relational database system. It is very powerful and chances are that you’ve been to many web sites that were using a MySQL database to show information to you. In fact, MySQL stores all the information on this blog.
Databases consist of tables, which consists of columns and rows of information. Here’s an example of a table:
Table Name: car_parts_inventory
| Row # | Part Name | Part ID |
| 1 | Radio | F5JG7 |
| 2 | Muffler | GG446 |
So there’s a very small table that stores information about car parts and their associated ID number. Some large databases may contain hundreds of thousands of rows.
I mentioned earlier that MySQL is a relational database, which basically means that two or more tables can share the same attribute. Let’s do a small example. In our database, we’ll store movie information and use a relation to organize which actors are in the movie.
First we have the table that stores our actors:
Table Name: actors
| actor_id | actor_name |
| 1 | Will Smith |
| 2 | Kevin Spacey |
And here’s the table that stores our movie information:
Table Name: movies
| movie_id | movie_name | movie_star_actor |
| 1 | Independence Day | 1 |
| 2 | The Pursuit of Happyness | 1 |
| 3 | Superman Returns | 2 |
| 4 | Hancock | 1 |
| 5 | Pay It Forward | 2 |
In our movie table we have a unique movie_id for each movie, then the movie’s name, and then a number that represents the actor_id (from the first table) who stars in the movie.
In our next tutorial, we’ll look at how to create our new database in MySQL.
Tags: introduction, mysql, php, tutorial
Windows 7 Released
Windows 7 was officially released yesterday and was the buzz on many blogs. I didn’t realize that people had operating system launch parties but hey, some people need any excuse to drink! Besides, that Windows cake looks tasty.
I’ll be upgrading soon, just because I like to have the latest version. I stuck with XP for years (after sticking with Windows ME for years, wow!) and switched to Vista with my new PC a few months ago. People are hopeful about Windows 7 — hopefully it works out better than the Vista release did.
New Tutorial
I added one new tutorial this week: PHP Function Return Values.
Site Restructure
In Wordpress, you have Posts and Pages. Posts are used for blogging and Pages are used for static content, like an About or Contact page. I’ve been using the Pages to post my tutorials, but I thought about it and decided I’ll switch to using the Posts for them.
This will allow new tutorials to show up in the blog feed instead of requiring me to make a special post about the new pages on the site. I don’t know why I didn’t think about this to begin with, but it should be better for all you readers out there!
Just added two new tutorials about creating and using functions in PHP. I’m still working on another one or two tutorials about functions in specific. Functions are so powerful and important in PHP that I could probably write dozens of articles about them, but these first few tutorials will give beginners what they need to know.
Here’s the first two:
1. PHP Function
A function allows you to reuse a block of code throughout your application.
2. PHP More With Functions
Continue learning more about the power of PHP functions.
These are listed in our Building PHP Scripts, along with most of our tutorials.
In our last Else and Elseif Statement tutorial, we saw an example of the PHP “Is Equal To” operator (==). In this tutorial, I’ll show you the most common PHP operators and what they do.
Operator: ==
Meaning: Is equal to
Example: <?php if (3 == 3) { echo “True.”; } ?>
Notes: In our example, 3 is in fact equal to 3, so our script will output “True.” Be aware of the subtle different between one equal sign (=) and two (==). They mean very different things in PHP. One equal sign is used to assign a value to something, like when you create a PHP variable (Jon: link). Two equal signs are used to ask or tell PHP that one thing is equal to another thing.
Operator: !=
Meaning: Is not equal to
Example: <?php if (3 != 4) { echo “True.”; } ?>
Notes: In our example, 3 is not equal to 4, so our script will output “True.” The exclamation point (!) is used in PHP to ask if something is “not”, so in this case, we’re asking if 3 is NOT equal to 4 by putting the exclamation point in front of the equal sign.
Operator: >
Meaning: Is greater than
Example: <?php if (4 > 3) { echo “True.”; } ?>
Notes: In our example, 4 is greater than 3. Most of us are familiar with the greater than sign (>) from math classes. This is primarily used with numbers. PHP has no personal opinions, no way of inherently knowing if (Brad Pitt > Tom Hanks) is true or not
Operator: <
Meaning: Is less than
Example: <?php if (3 < 4) { echo “True.”; } ?>
Notes: In our example, 3 is less than 4. Again, most of us are familiar with the less than sign (<) from math.
Operator: >=
Meaning: Is greater than or equal to
Example: <?php if (6 >= 5) { echo “True.”; } ?>
Notes: In our example, 6 is greater than 5, so this If Statement is true. We could also say that (5 >= 5) is true, because 5 is equal to 5.
Operator: <=
Meaning: Is less than or equal to
Example: <?php if (7 <= 8) { echo “True.”; } ?>
Notes: In our example, 7 is less than 8, so this If Statement is true. We could also say that (8 <= 8) is true, because 8 is equal to 8.
Operator: &&
Meaning: and
Example: <?php if ($cats && $dogs == “cute”) { echo “True.”; } ?>
Example: <?php if ($cats AND $dogs == “cute”) { echo “True.”; } ?>
Notes: && is another way of saying AND. Both examples above work equally well. Assuming we give our $cats and $dogs variables the value of “cute” (and who wouldn’t), these If Statements return true.
Operator: ||
Meaning: or
Example: <?php if ($cats || $dogs == “cows”) { echo “Woof!”; } ?>
Example: <?php if ($cats OR $dogs == “cows”) { echo “Meow!”; } ?>
Notes: || is another way of saying OR. Both examples above work equally well. Both examples would return false, because neither $cats or $dogs are equal to cows, even if they are very well fed.
Go back to Building PHP Scripts.
Tags: operators, php, programming, tutorial
PHP Loops are frequently used to repeat a set of statements and code. Loops can be set to repeat a specified number of times or they can be set to repeat until a certain condition is met. For example, a loop that echoes all the letters in the English alphabet will repeat 26 times, whereas a loop that echoes all of the products in a shopping cart will repeat until it finds no new items.
We’re going to look at three types of loops:
1. PHP While Loops
2. PHP For Loops
3. Foreach Loops
Go back to Building PHP Scripts.
Tags: loops, php, programming, tutorial