AN INTRODUCTION TO WEB PROGRAMMING Dr. Hossein Hakimzadeh Department of Computer and Information Sciences Indiana University South Bend, IN
HISTORY Developed by Michael Widenius. Initially release in 1995. Latest release in 2013. One of the most popular open source relational database systems. According to http://en.wikipedia.org/wiki/mysql as of 2008 MySQL, is the most widely used open source relational database management system. It runs as a server, and provides multi-user access to many databases. For a while the MySQL was owned and sponsored by a Swedish company MySQL AB, but it is currently owned by Oracle corporation. Written in C and C++, cross platform, GNU General Public License. http://www.mysql.com/ http://dev.mysql.com/doc/index-about.html MySQL 2
MYSQL AT IU SOUTH BEND My team has been using MySQL since 2004. Our first project (IU-EVAL) used MySQL as its backend database. Since that time, several other projects (e.g., IU- ALUMNI, IU-RETAIN, IU-ADVISE, and IU-READY) have been using the same database system. The MySQL database has proven to be extremely fast, reliable, and feature rich. MySQL 3
SOME TERMS? DBMS Table/File/Entity Row/Record/Tuple Column/Field/Attribute Cell (an individual value) Storage engine SQL (Structured Query Language) Command line interface MySQL admin interface MySQL 4
WHAT IS A DATABASE? Logically coherent collection of data with inherent meaning. A database is an integrated, shared repository of operational data of interest to an enterprise. MySQL 5
WHY A DATABASE? Controlled redundancy Restricted and authorized access (User, databases, tables, etc.) Persistent storage Ability to represent complex relationship among data. Enforce integrity constraints Backup and recovery Standardized query language (SQL) Etc.. MySQL 6
WHAT IS A DATA BASE MANAGEMENT SYSTEM? (DBMS) A collection of programs that enable the user to create and maintain a database. A DBMS provide the following capability: Defining the database (schema, define the attributes, their types, size, constraints, range, etc.) Populate the database (Add data to the database) Manipulate the database (query, update, generate reports, create views, join information) MySQL 7
AN INTRODUCTION TO WEB PROGRAMMING Dr. Hossein Hakimzadeh Department of Computer and Information Sciences Indiana University South Bend, IN
HOW DOES PHP TALK WITH MYSQL? When a client requests a file with a.php extension, the web server sends the request to the PHP interpreter for preprocessing. If the PHP code has any database interaction, PHP first connects to the database and performs the database operations, takes the result of those operations and composed the proper HTML to be sent back to the web server, which is then sent to the client. Image from: http://www.santarosa.edu/~jperetz/spring2012/dreamweaver/ MySQL 9
DATA MODELING MySQL 10
MYSQL Starting up XAMPP/Apache/MySQL. MySQL 11
CREATING THE CLUB DATABASE MySQL 12
SAMPLE SQL COMMANDS.. 1) CREATE DATABASE `club` ; 2) CREATE TABLE `club`.`member` ( `FirstName` VARCHAR( 32 ) NOT NULL, `LastName` VARCHAR( 32 ) NOT NULL, `Email` VARCHAR( 128 ) NOT NULL ) ENGINE = MYISAM ; 3) INSERT INTO `club`.`member` ( `FirstName`, `LastName`, `Email` ) VALUES ( 'Tom', 'Johnson', 'tjohnson@gmail.com' ), ( 'Mary', 'Adams', 'madams@yahoo.com' ); 4) ALTER TABLE `member` ADD PRIMARY KEY ( `Email` ) MySQL 13
AN INTRODUCTION TO WEB PROGRAMMING Dr. Hossein Hakimzadeh Department of Computer and Information Sciences Indiana University South Bend, IN
PHP INTERFACE TO MYSQL PHP provides 3 different APIs to MySQL database: 1. MySQL Original and a little outdated, based on C language (http://www.php.net/manual/en/book.mysql.php) 2. MySQLi OOP version (http://www.php.net/manual/en/book.mysqli.php) 3. PDO OOP, Multi-platform (works with MySQL, but it also works with other databases) (http://us3.php.net/manual/en/book.pdo.php) MySQL 15
TYPICAL DB APPLICATION: Step 1: Make a connection to MySQL Server Step 2: Check for Connection Errors Step 3: Create a new Database Step 4: USE the newly created database (Make it the active DB) Step 5: CREATE one or more Tables Step 6: INSERT, DELETE, UPDATE records optional Step 7: SELECT (retrieve) records from the tables Step 8: Close the connection to the MySQL server MySQL 16
MYSQL-I API $con = mysqli_connect(dbhost, user, password, dbname ); Before we can access data in a database, we must open a connection to the MySQL server. mysqli_connect_errno($con) Check to see if the connection is established. mysqli_query($con, $sql) Submit the SQL query to be processed by the database. mysqli_close($con ) The connection will be closed automatically when the script ends. To close the connection before, use the mysqli_close() function. MySQL 17
A SIMPLE MYSQL APPLICAITON 1) Open Connection $con=mysqli_connect("localhost", User","Password","my_db ) 2) Insert Data $sql="insert INTO Persons (Firstname, Lastname, Age) VALUES ('Sam', 'Chellokabob',35)"; mysqli_query($con, $sql); 3) Retrieve Data $sql="select * FROM Persons"; $result = mysqli_query($con,$sql); while($row = mysqli_fetch_array($result)) { echo $row['firstname']. " ". $row['lastname']; echo "<br />"; } 4) Close Connection mysqli_close($con); MySQL 18
MYSQL-I API $result = mysqli_query($con,$sql); Perform a SQL query on the database and return true if successful. If the query produces a result-set, one can use the following functions: mysqli_num_rows($result) to get the number of rows. mysqli_field_count($con) to get the field count. $row = mysqli_fetch_row($result); to get the rows. mysqli_free_result($result); to free the result-set. MySQL 19
AN INTRODUCTION TO WEB PROGRAMMING Dr. Hossein Hakimzadeh Department of Computer and Information Sciences Indiana University South Bend, IN
TYPICAL DB APPLICATION: Step 1: Make a connection to MySQL Server Step 2: Check for Connection Errors Step 3: Create a new Database Step 4: USE the newly created database (Make it the active DB) Step 5: CREATE one or more Tables Step 6: INSERT, DELETE, UPDATE records optional Step 7: SELECT (retrieve) records from the tables Step 8: Close the connection to the MySQL server MySQL 21
SAMPLE DATABASE (PHP API) Open a Connect: $con=mysqli_connect("localhost","a340user","pass123word"); Check for errors: mysqli_connect_errno($con) mysqli_connect_error() Create a Database: CREATE DATABASE IF NOT EXISTS my_db; mysqli_query($con,$sql) Make an Active Database: USE my_db; Create a Table: CREATE TABLE IF NOT EXISTS persons(firstname CHAR(30),Lastname CHAR(30),Age INT);"; mysqli_query($con,$sql) Insert Record(s): INSERT INTO Persons (Firstname, Lastname, Age) VALUES ('Sam', 'Chellokabob',35);"; mysqli_query($con, $sql); Retrieve / Display Record(s): SELECT * FROM Persons $row = mysqli_fetch_array($result) Close the Connection: mysqli_close($con); MySQL 22