Home


PHP/MySQL Tutorial

PHP/MySQL Tutorial
C. Masi 7/01/2011

  • What is PHP? What is MySQL? - Review Definitions

  • Set up working environment
    • Must have write access to a server with PHP and MySQL installed
    • Can set up a local webserver on your own windows machine using XAMPP
    • Test to see whether php and mysql are available on the server using phpinfo()

  • MySQL
    • Your hosting service should provide a link to phpMyAdmin, a user interface that allows you to create MySQL databases, tables, and queries and import and export data.
    • Become familiar with phpMyAdmin. Below are some good tutorials to help you do so:
      • http://www.reg.ca/faq/PhpMyAdminTutorial.html
      • http://www.youtube.com/watch?v=1-81n_vuwug
      • http://php.about.com/od/learnmysql/ss/create_tables.htm
    • Create your first database. This may already have been done for you by your hosting service. If not, you can do it using phpMyAdmin (if you have a high enough access level) OR you can do it using the control panel provided by your hosting service. Follow the instructions provided by your hosting service to create your first database.
    • Create your first test table - in the example below, we have named the table patrons.
      • Rules of thumb for object names (db, table and field names):
        • use lower case except if good reason for upper
        • no spaces or special characters
          • http://www.informit.com/articles/article.aspx?p=30875
        • avoid reserved words
          • http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
        • first field in table should be id = primary key, auto increment
    • Two ways to create table in phpMyAdmin – using the dialogue box or running an sql query. Example query:
      • CREATE TABLE patrons (
        id int(11) NOT NULL auto_increment,
        name varchar(30) NOT NULL,
        address varchar(60),
        city varchar(30),
        state varchar(2),
        zip varchar(10),
        PRIMARY KEY (id)
        ) AUTO_INCREMENT=1 ;

        Once you have created the table, add some test data using phpMyAdmin.

    • Now back up your table (export it) and restore it to a new name (import it) using phpMyAdmin!

  • PHP
    • Is PHP available on your server? MySQL? What versions?
      • On a blank page type <?php phpinfo()?> and save it to your server as phpinfo.php.
      • View example of results
    • PHP code blocks always begin with <?php and end with ?>
    • PHP code can be inserted into HTML, much like JavaScript can be inserted, but PHP code will only work if the file extension is correct, e.g. .php, and it is published to a server that is PHP enabled
    • Can use notepad, wordpad etc and then save as filename.php
    • Each line of PHP coding ends with a ';'
    • Comment lines in php are preceded by '//'
    • See sample code (mysql_select_display.php) to display the data in the table created above.
    • Note: The above sample is very basic and is just to demonstrate how database connections and queries work in PHP. The best practice is to store the database information in a separate file and reference it using an "include" statement, but that is for a later tutorial.
    • Copy the code from the above sample to your web server (in htdocs, www, or designated root of your site) and try it out!

  • PHP/MySQL Resources