web engineering (lecture 11) php part-3 - students founder€¦ · php mysql introduction mysql is...
TRANSCRIPT
Web Engineering
(Lecture 11)
PHP
part-3
By:
Mr. Sadiq Shah
Lecturer (CS)
Class: BS(IT)-6th semester
PHP MySQL Introduction
MySQL is a database.
The data in MySQL is stored in database objects called tables.
A table is a collection of related data entries and it consists of columns and rows.
For example a company may have a database with the following tables: "Employees", "Products", "Customers" and "Orders".
Database Tables
A database most often contains one or more tables.
Each table is identified by a name (e.g. "Customers"
or "Orders"). Tables contain records (rows) with
data.
Following is an example of Database Table.
It has two records and four columns.
Table name: Student
ID NAME Fname GPA
1 Peter Roger 3.5
2 Roger Peterson 3.4
PHP MySQL Connection to Database
Before accessing data in a database, we must create a
connection to the database.
Function used for connection is mysql_connect() .
Syntax:
mysql_connect(servername,username,password);
Servername“: Specifies the server name for connection.
Default value is "localhost”
Username: Specifies the username to log in with. Default
value is “root”.
Password: Specifies the password to log in with. Default
value is “ ” which means empty
mysql_connect()
Example
<?php $con = mysql_connect("localhost",”root",”"); if (!$con) // !$con means false { die('Could not connect: ' . mysql_error()); }
mysql_close($con); ?>
The above example creates a connection to PHP Mysql
Die() and mysql_error() functions
Die() is used to print or display a message to
user and exits the current PHP script.
mysql_error() function returns the actual php
mysql error. It plays an important role in
finding syntax errors.
mysql_close() is used for closing the mysql
connection.
PHP MySQL: Creating a Database
The CREATE DATABASE statement is used
to create a database in MySQL.
mysql_query() function is used to execute all
kind of queries in PHP.
Syntax:
CREATE DATABASE database_name
mysql_query() function is used to execute all
kind of queries in PHP.
PHP MySQL: Create Database
Example
1. First create a connection
2. Then create a database named bit_6
<?php
$con = mysql_connect("localhost",”root",”");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_query("CREATE DATABASE bit_6”);
?>
Now check phpmyadmin to see database bit_6.
PHP MySQL Create Table
Following statement is used to create table inside a database in PHP.
Syntax is:
CREATE TABLE table_name ( column_name1 data_type(length), column_name2 data_type(length), column_name3 data_type(length), .... )
PHP MySQL Create Table
Example
Let we want to create a table in database bit_6.
Suppose the table name is student with three columns
ID, name, gpa.
mysql_select_db() function selects a particular
database for accessing.
<?php
mysql_connect("localhost",”root",”");
mysql_select_db(“bit_6");
mysql_query("CREATE TABLE student (ID int(15),
name varchar(15),
gpa float(15))“);
?>
Primary Keys and Auto
Increment Fields
A primary key is used to uniquely identify the rows
in a table.
Each primary key value must be unique within the
table.
The primary key field cannot be null.
AUTO_INCREMENT automatically increases the
value of the field by 1 each time a new record is
added.
Primary Keys and Auto
Increment Fields(example)
<?php
mysql_connect("localhost",”root",”");
mysql_select_db(“bit_6");
mysql_query("CREATE TABLE student (
ID int(15) AUTO_INCREMENT PRIMARY KEY,
name varchar(15),
name varchar(15)) “);
?>
Inserting Data to a Table
We can insert records to a table using the following statements.
Syntax
INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)
Or
INSERT INTO table_name VALUES (value1, value2, value3,...)
Inserting Data to a Table
Suppose the table name is student with three columns ID, name, gpa.
The following example inserts a record.
Example
<?php
mysql_connect("localhost",”root",”");
mysql_select_db(“bit_6”);
mysql_query("INSERT INTO student (ID, name, gpa) VALUES (1, „Farhan', 3)");
?>
End of Lecture