- Shopping Bag ( 0 items )
Step-by-step lessons for using PHP and MySQL in a unique book-and-video combination
Assuming no previous experience with PHP or MySQL, this book-and-video package is ideal reading for anyone who wants to go beyond HTML/CSS in order to provide clients with the most dynamic web sites possible. The approachable tone breaks down the basics of programming and PHP and MySQL in individual lessons starting with the installation of the programs necessary to run PHP. You begin with a ...
Step-by-step lessons for using PHP and MySQL in a unique book-and-video combination
Assuming no previous experience with PHP or MySQL, this book-and-video package is ideal reading for anyone who wants to go beyond HTML/CSS in order to provide clients with the most dynamic web sites possible. The approachable tone breaks down the basics of programming and PHP and MySQL in individual lessons starting with the installation of the programs necessary to run PHP. You begin with a static web site and then watch and learn as PHP functionality is added as you work through the lessons.
When working with databases, the MySQL database is introduced with demonstrations that show how to interact with it. The accompanying videos enhance your learning experience, as each lesson in the book is portrayed in the video exercises.
• Getting started with PHP
• Setting up your workspace
• Adding PHP to a web page
• Learning PHP syntax
• Working with variables
• Debugging code
• Working with complex data
• Making decisions
• Repeating program steps
• Learning about scope
• Reusing code with functions
• Creating forms
• Introducing object-oriented programming
• Defining classes
• Using classes
• Using advanced techniques
• Handling errors
• Writing secure code
• Introducing databases
• Introducing MySQL
• Creating and connecting to the
• Creating tables
• Entering data
• Selecting data
• Using multiple tables
• Changing data
• Deleting data
• Preventing database security issues
• Creating user logins
• Turn the case study into a content management system
Note: As part of the print version of this title, video lessons are included on DVD. For e-book versions, video lessons can be accessed at wrox.com using a link provided in the interior of the e-book.
Your computer needs to be able to run as a web server with PHP and MySQL. XAMPP is a package of software that installs the web server, PHP, and MySQL for you. You learn how to download and install XAMPP in this lesson.
If you already have a web server with PHP and MySQL running on your computer, you do not need XAMPP. Other packages that fulfill the same need are WAMPServer and MAMP.
You also need a text editor that can produce plain-text files. You learn how to download and install Eclipse PDT in this lesson. Some other text editors that you can use are Adobe's Dreamweaver in code mode, Notepad, TextWrangler, or NetBeans.
XAMPP stands for whatever operating system you have: (X), Apache (A), MySQL (M), PHP (P), and Perl (P). Separate packages are available for each of the different operating systems such as Windows, Mac OS X, or Linux. This lesson covers installing the Windows and Mac versions.
XAMPP is intended for local development work. It is not set up for running production websites.
Installing XAMPP on a Windows PC
This section walks you through downloading the proper XAMPP package and installing it on your Windows PC. If you have a Mac, skip forward to the section "Configuring XAMPP on Mac OS X."
1. Go to the Apache Friends website at www.apachefriends.org/en/xampp.html.
2. Locate the section labeled XAMPP for Windows and click the title. Scroll down to the Download section that lists the versions available for download. See Figure 1-1.
3. You have a choice of three ways to install this package: via the installer, via a ZIP file, or via a 7zip file. The easiest way to change options is to use the installer, but you are more likely to encounter problems. Because you are using the defaults, use the ZIP version. Click the ZIP link and save the ZIP file.
4. Unzip all the files to c:\. The ZIP file contains a folder called xampp that holds all the folders and files so unzipping to the c: drive creates the c:\xampp folder.
5. The program you use is c:\xampp\xampp-control.exe. In Windows Explorer, right-click the file and select Create Shortcut. Drag that shortcut to your desktop.
6. Double-click the XAMPP Control desktop icon you just created. The Control Panel is displayed.
7. To start XAMPP, first start the Apache web service by clicking the Start button next to Apache. Then start MySQL by clicking the Start button next to MySQL. You do not need to start FileZilla or Mercury. When you click the Start buttons, they change to Stop buttons to indicate that the processes are running.
8. To stop XAMPP, click the Stop button next to MySQL and then click the Stop icon next to Apache.
9. To test that XAMPP is properly working, go to your browser and enter http://localhost/ xampp. You should see a screen similar to Figure 1-3.
If the installation is successful, skip to the "Configuring XAMPP" section later in this lesson. Otherwise, check out the "Troubleshooting Your XAMPP Installation" section that follows the "Installing XAMPP on Mac OS X" section.
Installing XAMPP on Mac OS X
This section walks you through downloading the proper XAMPP package and installing it on your Mac OS X system. If you are using a Windows PC, you used the prior section to install XAMPP so you can jump forward to the "Configuring XAMPP" section.
1. Go to the Apache Friends website at www.apachefriends.org/en/xampp.html.
2. Locate the section labeled XAMPP for Mac OS X and click the title. Scroll down to find the section labeled Installation in 4 Steps. See Figure 1-4.
3. Click XAMPP Mac OS X. You want the Universal Binary, not the Developer Package. Click OK to save the file when asked.
4. Open the .dmg file you just saved. Drag the XAMPP icon over to the Applications icon as shown in Figure 1-5.
5. Find the XAMPP Control.app in /Applications/XAMPP/Xamppfiles. This is the application file that you use to start and stop XAMPP and you will find it convenient to add it to your dock. The first time you open it you receive the standard warning about using files from the Internet. Click the Open button to start the Control Panel. The Control Panel looks like Figure 1-6.
6. To start XAMPP, first start the Apache web service by clicking the Start button next to Apache. Then start MySQL by clicking the Start button next to MySQL. You do not need to start FTP. When you click the Start buttons, they change to Stop buttons to indicate that the processes are running.
7. To stop XAMPP, click the Stop button next to MySQL and then click the Stop button next to Apache.
8. To test that XAMPP is properly working, go to your browser and enter http://localhost/ xampp. You should see a screen similar to Figure 1-7.
Troubleshooting Your XAMPP Installation
Usually, XAMPP installs easily. Sometimes, however, you can run into issues. The Apache Friends have a forum where you can find answers to many problems at www.apachefriends.org/f/ viewforum.php?f=34.
The Mac OS X ships with Apache. Apache works by listening on a specific port. If you run two copies of Apache, both listening to the same port, you will have problems. The default port is 80 and the common alternate port to use is 8080. If you need both, change the port on one of them and restart Apache.
If you want to change the port in XAMPP, go to /Applications/XAMPP/xamppfiles/etc/httpd .conf and change Listen 80 to Listen 8080. Stop and restart Apache for the change to take effect. If you cannot get into the XAMPP control to stop and start Apache, shut down your Mac and restart it.
If you want to change the port in the pre-installed Apache, go to etc/Apache2/http.conf and change Listen 80 to Listen 8080. To get to this hidden file, go to Finder and press Shift+Command+G and then enter \etc. You need to restart the pre-installed Apache. The easiest way to do that is to shut down your Mac and restart it.
Skype is another program that might conflict with port 80. If you have problems, look in the Skype Advanced section of Tools/Options (on the PC) or Preferences (on the Mac) and be sure it isn't using port 80 for incoming or alternative ports.
Now that you have successfully installed XAMPP on your Windows PC or Mac, make sure XAMPP is running and then call up XAMPP in your browser. The address to call up XAMPP is http://localhost/xampp. A screen similar to Figure 1-8 displays.
You need to create a password on MySQL. Some programs do not allow you to use MySQL unless MySQL has a password, for security reasons. Click the phpMyAdmin link on the left-side navigation under Tools to open the page shown in Figure 1-9.
Click Privileges on the top menu. You see a table of the users. Click the Edit icons next to the users. Scroll down to find the Change Password box as shown in Figure 1-10.
Enter a password and click Go. Do this for each of the users with All Privileges.
Now that you've added a password to MySQL, you need to change the configuration in XAMPP for phpMyAdmin so that it can access the database. The configuration file is in c:\xampp\phpMyAdmin\ config.inc.php on the Windows PC or in /Applications/XAMPP/xamppfiles/phpmyadmin/ config.inc.php on the Mac. Find the following code:
/* Authentication type */ $cfg['Servers'][$i]['auth_type'] = 'config'; /* Server parameters */ $cfg['Servers'][$i]['host'] = 'localhost'; $cfg['Servers'][$i]['user'] = 'root'; $cfg['Servers'][$i]['password'] = '';
Change the password to your new password. For instance, if your new password is !xYz72g, the change looks like the following:
$cfg['Servers'][$i]['password'] = '!xYz72g';
Restart the XAMPP server by going into the Control Panel and stopping first MySQL and then Apache. Restart by starting Apache and then restarting MySQL.
Call up XAMPP in your browser and see that you can open phpMyAdmin.
INSTALLING YOUR EDITOR
You need a text editor for programming. Word processing editors such as Word change your code and add extraneous codes and characters that invalidate your program, so you should not use them. Possible text editors are Notepad, TextWrangler, Dreamweaver in the code mode, NetBeans, or Eclipse.
A good text editor for PHP is Eclipse PDT. It has syntax checking, auto-completion for commands, color syntax coding, debugging, and other features that become important as you do more complex PHP programming.
To install Eclipse PDT, go to http://www.eclipse.org/pdt/downloads/ to download the program. You see a screen similar to Figure 1-11.
Find the All-in-One package for your operating system and click it. You are given a choice of mirrors from which you can download the package, as shown in Figure 1-11. Click the mirror displayed (which in this figure is Georgia Tech) and save the file when requested.
Unzip the file in an appropriate folder. In Windows a good folder is c:\eclipse. It does not need to be installed. On the Mac, put it in the Applications directory.
The program file is eclipse.exe in the eclipse folder. Make a shortcut on your desktop or add it to your dock (on the Mac) so you can find it easily.
CONFIGURING YOUR WORKSPACE
Now that you've installed the programs, you need to do some configuring.
Preparing a Place to Put Your Files
The first thing you need to do is decide where you are going to put your files. By default the Apache web server looks for web files in the htdocs folder. On a Windows PC, this is directly off where you installed XAMPP. If you installed XAMPP in c:/xampp, then your htdocs file is in the c:/xampp/ htdocs folder. On a Mac the path is /Applications/XAMPP/xamppfiles/htdocs.
If you are going to be doing a lot of development work, you should change this default and set up virtual hosts so that you can put your files in more convenient places. However, setting up virtual hosts is beyond the scope of this book, so use the default htdocs folder.
If you are on a Windows PC, skip forward to the next section, "Using Eclipse for the First Time."
On the Mac OS X you need to change the permissions to the htdocs folder in order to add folders and files to it. Open Finder, browse to /Applications/XAMPP/xamppfiles, and select htdocs as shown in Figure 1-12.
Press Command+I to display the htdocs Info as shown in Figure 1-13.
Click the arrow next to Sharing & Permissions to expand the section as shown in Figure 1-14.
You need to unlock the padlock before you are allowed to make changes to the permissions. Click the padlock in the lower-right corner. Enter your administrator password for the Mac when asked.
Now you can click the Privilege drop-down for the admin. Change from Read Only to Read & Write. Your permissions should look similar to Figure 1-15.
Click the padlock again to relock the permissions.
Using Eclipse for the First Time
The first time you go into Eclipse, you have to identify an Eclipse workspace. See Figure 1-16. This workspace is the place that you put your files. You use the htdocs folder.
Use the Browse button to locate and accept the htdocs folder. On the PC, if you installed XAMPP in c:/xampp, your htdocs file is in the c:/xampp/htdocs folder. On a Mac, the path is /Applications/XAMPP/xamppfiles/htdocs. See Figure 1-17. Click OK.
Eclipse displays a splash screen along with a request for permission for Eclipse to collect and send usage information, as shown in Figure 1-18.
Uncheck the box if you do not want to have this information collected and sent. Click OK and then close the tab for the splash screen. You see the main workspace for Eclipse, as shown in Figure 1-19.
Across the top of the window, Eclipse lists the path to the workspace you are in. If you use virtual hosts you can create multiple workspaces, although you can have only one open at a time. To switch between workspaces, or to add a new workspace, use File Switch Workspaces. You use only one workspace in this book.
Within the workspace, you have projects. All your folders and files are created inside these projects. To create a project for this book, click File New PHP Project. You see a screen similar to Figure 1-20.
Type in a project name of php24. You can use what you want here, but this is part of the address you use to call your programs, so short and uncomplicated is best. Leave all the rest as the defaults and click Finish. You should see a screen similar to Figure 1-21.
Notice that you now have the project php24 listed in the left window. This is the PHP Explorer. It works like the Windows Explorer, showing a hierarchical view of the folders and files within the different projects. Just as in Windows Explorer, you use this to open and select files and perform other actions on them. Right-clicking php24 displays a menu as shown in Figure 1-22.
Here's what some of these options do:
* New: Use this to create new folders and files.
* Copy/Paste/Delete: These work as you would expect.
* Refactor: This is where Rename and Move are hidden.
* Refresh: Use this if you've added, renamed, or moved files outside of Eclipse.
* Debug As: You learn more about this in Lesson 5.
* Validate: This is for validating files without needing to open them.
* Compare With: This compares files either with another file or with an earlier version of the same file. Files are automatically posted to Local History so you can roll back to a prior version. See Options (in Windows) or Preferences (Mac) if you want to change how many copies and how long they are kept.
* Properties: This is for quite a bit of Meta information.
In the center of the screen is a blank window. This is where you edit your files. Over to the right is another window with tabs in it. The Outline tab shows you the outline of whatever file you have open and selected and can be used to navigate to various sections in the file. The bottom of the screen has another window with tabs. The Problems tab shows validation errors and the Tasks tab lists to-do's you have put inside your files and enables you to navigate directly to them.
This full screen that you see is called by Eclipse a "perspective." This particular perspective is the PHP perspective. Later you use a Debug perspective. Perspectives, which can be customized in Windows Customize Perspectives, enable you tailor your work area to best suit the project you are working on.
In this Try It, you create a .php file and run it to test your PHP setup. This program runs a very handy function to see how you have your PHP configured. This is even handier for hackers so it is good practice not to have a file running it on your system. For a local system it is not a problem however, because you delete it when you are done.
To run PHP files locally, you need to set up your computer so it can process PHP and run MySQL. This lesson shows you how to do that using XAMPP.
You also need to be able to create the PHP files themselves. You can do this with any editor that can produce plain-text files, including Notepad, TextWrangler, Dreamweaver in the code mode, NetBeans, or Eclipse. You learned in this lesson how to install the Eclipse PDT.
You should have a folder called php24 in the htdocs folder where you put your code files. If you created the Eclipse workspace, the folder was created for you automatically.
This is the code you need in the .php file:
Be sure that the Apache server is running when you run the program.
Excerpted from PHP and MySQL 24-Hour Trainer by Andrea Tarr Copyright © 2012 by John Wiley & Sons, Ltd. Excerpted by permission of John Wiley & Sons. All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.
SECTION I: GETTING STARTED WITH PHP
LESSON 1: SETTING UP YOUR WORKSPACE 3
LESSON 2: ADDING PHP TO A WEB PAGE 23
LESSON 3: LEARNING PHP SYNTAX 33
LESSON 4: WORKING WITH VARIABLES 45
LESSON 5: DEBUGGING CODE 57
LESSON 6: WORKING WITH COMPLEX DATA 71
SECTION II: WORKING WITH PHP CONTROLS, FUNCTIONS, AND FORMS
LESSON 7: MAKING DECISIONS 91
LESSON 8: REPEATING PROGRAM STEPS 107
LESSON 9: LEARNING ABOUT SCOPE 119
LESSON 10: REUSING CODE WITH FUNCTIONS 125
LESSON 11: CREATING FORMS 141
SECTION III: OBJECTS AND CLASSES
LESSON 12: INTRODUCING OBJECT-ORIENTED PROGRAMMING 161
LESSON 13: DEFINING CLASSES 167
LESSON 14: USING CLASSES 177
LESSON 15: USING ADVANCED TECHNIQUES 187
SECTION IV: PREVENTING PROBLEMS
LESSON 16: HANDLING ERRORS 205
LESSON 17: WRITING SECURE CODE 217
SECTION V: USING A DATABASE
LESSON 18: INTRODUCING DATABASES 227
LESSON 19: INTRODUCING MYSQL 239
LESSON 20: CREATING AND CONNECTING TO THE DATABASE 263
LESSON 21: CREATING TABLES 275
LESSON 22: ENTERING DATA 295
LESSON 23: SELECTING DATA 313
LESSON 24: USING MULTIPLE TABLES 331
LESSON 25: CHANGING DATA 343
LESSON 26: DELETING DATA 361
LESSON 27: PREVENTING DATABASE SECURITY ISSUES 387
SECTION VI: PUTTING IT ALL TOGETHER
LESSON 28: CREATING USER LOGINS 399
LESSON 29: TURN THE CASE STUDY INTO A CONTENT MANAGEMENT SYSTEM 419
LESSON 30: CREATING A DYNAMIC MENU 443
LESSON 31: NEXT STEPS 461
APPENDIX : WHAT’S ON THE DVD? 463