Free web design training service and resources by the Rochester Web Wizard @ Data Info Magic

Free web design training guide and resources by the Rochester Web Wizard. Sponsored by Data Info Magic.

Lesson 5 : Getting Started Writing MySQL Queries

MySQL is a relational database management system (RDBMS) which has more than 6 million installations. The program runs as a server providing multi-user access to a number of databases.

The project's source code is available under terms of the GNU General Public License, as well as under a variety of proprietary agreements. MySQL is owned and sponsored by a single for-profit firm, the Swedish company MySQL AB, now a subsidiary of Sun Microsystems, which holds the copyright to most of the codebase.

MySQL is commonly used by free software projects which require a full-featured database management system, such as WordPress, phpBB and other software built on the LAMP software stack. It is also used in very high-scale World Wide Web products including Google and Facebook.

To get started with writing PHP scripts, try writing your first PHP script. You really need to strongly consider purchasing a reference book. The best book resource for PHP is the Sams Teach Yourself MySQL in 10 Minute by Chris Newman. It is inexpensive (less than $20) short and consice. It is available almost anywhere books are sold including Amazon and Barnes & Noble online. This is the only reference book you will ever need for MySQL code.

 

Reminder: You need to have XAMMP including the Apache Server and PHP installed and running to try PHP locally. Your HostMonster account already has Apache Server and PHP set up and ready to go but you need to request setup for a MySQL database.There is a one time setup charge and no monthly costs thereafter. For other Hosts you will need to check on their particular procedure and whether they support it. All you need to do is change the file extention to .php and add your PHP code to the HTML. A PHP page can still run HTML code. You just nee to start <?php and stop ?> the PHP code.

 

PHP and MySQL are made for each other!

PHP is made to perfectly allow users to interact with PHP on a web page while not even being aware that the are doing any MySQL at all. That is because it is a Server-Side Script that runs on the Host Server and not on the Local Machine like HTML and Java Script. Each part is completely it's own element and runs independently, however. It is good to learn each one alone before trying to combine them. You will be on thought overload and start to lose focus, otherwise. Then when you add in HTML code to the page, you will have three differnt syntax models going on at the same time and it becomes very easy to lose your way. That is the main problem with most other tutorials. They don't separate the skills first and then combine them when you have become comfortable.

 

 

 

MySQL is a Database Management System

You can execute MySQL queries directly on the local machine using just the SQL server installed with XAMPP. You need PHP to execute MySQL queries on a remote server. Therefore we will stck with the localhost for this section. Even though you may be impatient and want to combine the two topics, take the time to go through the MySQL commands alone first. Then it will be easy to combine them later and you will understand what it is that you are doing.

You can use A MySQL editor phpMyAdmin or DreamCoder for MySQL for this section. They are both free. I would suggest learning both. They do the same thing in different ways. phpMyAdmin is what HostMonster and most other hosts use. DreamCoder is handy for for rapidly developing databases.

You can execute Queries (MySQL commands) directly on the server or run a SQL Script (A script that executes a series of MySQL commands all at once.) also known as a SQL Dump (Code generated by the Server that can be run on a server to recreate your MySQL database.) It sounds more confusing than it is.

If you have ever used a database like Microsoft Access then it wil be relatively simple to learn the new syntax. Once again, it uses the basic syntax shared by most current computer languages like Java and C# for relational syntax. If you know any of these languages then you no doubt have done etxfile manipulation code already.

Disclaimer: If you don't have any programming or database experience at all then you need to get some now or you may struggle with learning the syntax and concepts at the same time.

 

 

 

 

Here are some simple sample scripts

You can get the necessary information to connect from your hosting company. Don't worry about that until you combine it with the PHP in the next lesson.

First you will need to learn to create a database, create a table in it, create columns and properties for your table and add data.

Next you will need to run queries on your table with various outputs.

The 4 items that you must always know when dealing with a database on the local server (Apache installed on your machine) are:

1. Host is localhost

2. User Name is root

3. Password is can be left blank on the local machine so don't enter one initially.

4. Database name is whatever you choose and Table is also. One database may have many tables.

They can be the same on the remote server (The server set up by HostMonster or your current hosting company), except ALWAYS USE A PASSWORD AND SET YOUR USER PERMISSIONS AS STRICTLY AS POSSIBLE!!!!!

You can go through all of the examples in the Sams Teach Yourself MySQL in 10 Minute pretty rapidly, but it will probably take considerably more than 10 minutes! To save you time, I have compiled most of them in one document below. Each statement is meant to be executed individually. You can also use DreamCoder to speed things up but hold off until you know how to wite the code directly first. You will use the Exact Same Code in your PHP code later.

Comments are in parentheses and pink. Do Not Type the comments!!!


Don't bother with the DOS window.

To execute SQL commands in DreamCoder: Tools/SQL editor/Choose database.

Type in the code and Run SQL (The first icon on the right just above the window.) You will see the results in the window below it.


You will need to create the ijdb database and joketable with the properties below or run the script below.

/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
DROP TABLE IF EXISTS `ijdb`.`joketable`;
CREATE TABLE `ijdb`.`joketable` (
`id` int(11) NOT NULL auto_increment,
`joketext` text,
`jokedate` date default '0000-00-00',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;

joketable

Then try the following MySQL queries. Statements always end in a semicolon ; just like C++ and Java

USE ijdb;
SELECT joketext FROM joketable;
SELECT joketext, jokedate FROM joketable;
SELECT id,joketext, jokedate FROM joketable;
SELECT * FROM joketable;(shows all data)
DESCRIBE joketable;(shows design properties)
SHOW DATABASES;
SHOW TABLES; (shows tables in that database)
SHOW TABLES FROM addressbook; (shows tables in different database)
SHOW COLUMNS FROM joketable;


 

You will need to create the addressbook database and contacts table

/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
DROP TABLE IF EXISTS `addressbook`.`contacts`;
CREATE TABLE `addressbook`.`contacts` (
`id` int(6) NOT NULL auto_increment,
`first` varchar(15) NOT NULL,
`last` varchar(15) NOT NULL,
`phone` varchar(20) NOT NULL,
`mobile` varchar(20) NOT NULL,
`fax` varchar(20) NOT NULL,
`email` varchar(30) NOT NULL,
`web` varchar(30) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `id_2` (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=38 DEFAULT CHARSET=latin1;

/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;

address

USE addressbook; (to switch databases)
SHOW COLUMNS FROM contacts

SELECT * FROM contacts WHERE last='Ross';
SELECT * FROM contacts WHERE last!='Doe';
SELECT * FROM contacts WHERE last>'F';
SELECT * FROM contacts WHERE id>=10;
SELECT * FROM contacts WHERE last>first;
SELECT first,last FROM contacts ORDER BY last,first;
SELECT first,last FROM contacts ORDER BY last DESC;
SELECT * FROM contacts WHERE last>'F' AND id<25 OR first='Donald';
SELECT * FROM contacts WHERE last='Freeloader' OR last='Snowman';
SELECT * FROM contacts WHERE last  IN('Freeloader','Snowman');   (same as above)
SELECT * FROM contacts WHERE last NOT  IN('Freeloader','Snowman');   (everyone except)
SELECT * FROM contacts WHERE last BETWEEN 'Freeloader' AND 'Snowman';
SELECT * FROM contacts WHERE last NOT BETWEEN 'Freeloader' AND 'Snowman';
SELECT * FROM contacts WHERE last='Ross' and id<10 OR first = 'George'; (AND has higher precedence than OR which is higher than NOT)
SELECT * FROM contacts WHERE last='Ross' and id<10 OR first = 'George' LIMIT 2;
FROM contacts WHERE last='Ross' and id<10 OR first = 'George' LIMIT 4,2; (skips 4 rows before returning max. of 2 rows)


 


There are many more advanced queries in the reference book Sams Teach Yourself MySQL in 10 Minutes by Chris Newman. You will quickly get the hang of the syntax.

Imprtant Tip: Even though MySQL code is not case sensitive nor does it require indenting, make sure to follow the conventions for readibility, especially for yourself.

 

Now is the time to catch your breath and enjoy your accomplishments! Good luck and have fun.

Looking Ahead: Next we will combine the HTML, PHP and MySQL code.

Go on to Lesson 6 (Available Now!)

 
Back to Main Menu