You are on page 1of 17

PHP CONNECTING WITH MYSQL

Relational Databases
Relational Database: A method of structuring data as tables associated to each other by shared attributes.
A table row corresponds to a record; a column corresponds to an attribute (field) of the record

Relational databases typically use Structured Query Language (SQL) to define, manage, and search data

Wikipedia: http://en.wikipedia.org/wiki/Relational_database

Database Software
Oracle Database
Microsoft SQL Server (powerful) and Microsoft Access (simple) IBM DB2

PostgreSQL (powerful/complex free open-source database system)


MySQL (simple free open-source database system)
Many "LAMP" servers run Linux, Apache, MySQL, and PHP Wikipedia is run on PHP and MySQL (Wikipedia is the #5 site on the web and serves 450 million different people every month with billions of page views) We will use MySQL in this course

http://www.mysql.com/

Database Design
Database Design: the act of deciding the schema for a database
Database Schema: a description of what tables a database should have, what columns each table should contain, which columns' values must be unique, etc.

First Database Design

What's good and bad about this design? Uses only one table, but contains redundancy

Second Database Design

Splitting data into two tables (linked by Student ID) avoids redundancy This is also called normalizing the database Normalized tables are often linked by unique integer IDs

Structured Query Language (SQL)


SELECT name FROM Student WHERE SID = 456;
INSERT INTO Grade VALUES ('123', 'CPS130', 'C+');

A language for searching and updating a database A standard syntax that is used by all database software (with minor incompatibilities)

Connection to a MySQL Database


Before you can access data in a database, you must create a connection to the database.
In PHP, this is done with the mysql_connect() function. mysql_connect(servername,username,password);

See: http://www.w3schools.com/php/php_mysql_intro.asp

Connection to a MySQL Database


In the following example we store the connection in a variable ($con) for later use in the script. The "die" part will be executed if the connection fails:
<?php $con = mysql_connect("localhost","root","pinsql"); if (!$con) { die('Could not connect: ' . mysql_error()); } // some code mysql_close($con); ?>

The connection will be closed automatically when the script ends. To close the connection before, use the mysql_close() function.

Create a Database
To create a MySQL database you can use:
CREATE DATABASE database_name To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

You can also create database using software like:


phpMyAdmin MySQL Administrator

Create a Database
<?php $con = mysql_connect("localhost", "root", "pinsql"); if (!$con) { die('Could not connect: ' . mysql_error()); } if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); } mysql_close($con); ?>

Create a Table
The CREATE TABLE statement is used to create a table in MySQL.
CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type, .... )

We must add the CREATE TABLE statement to the mysql_query() function to execute the command.

Create a Table
$sql = "CREATE TABLE Persons ( personID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(personID), FirstName varchar(15), LastName varchar(15), Age int )";
mysql_query($sql,$con);

Insert Data Into a Database Table


<?php $con = mysql_connect("localhost", "root", "pinsql"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Peter', 'Griffin', '35')"); mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire', '33')");

mysql_close($con); ?>

Select Data From a Database Table


<?php $con = mysql_connect("localhost", "root", "pinsql"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Persons"); while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName'] . "<br />"; } mysql_close($con); ?>

Update Data in a Database Table


<?php $con = mysql_connect("localhost", "root", "pinsql"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query("UPDATE Persons SET Age = '36 WHERE FirstName = 'Peter' AND LastName = 'Griffin'"); mysql_close($con); ?>

Delete Data in a Database Table


<?php $con = mysql_connect("localhost", "root", "pinsql"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query("DELETE FROM Persons WHERE LastName='Griffin'"); mysql_close($con); ?>

You might also like