Top Banner
Web Engineering (Lecture 11) PHP part-3 By: Mr. Sadiq Shah Lecturer (CS) Class: BS(IT)-6 th semester
15

Web Engineering (Lecture 11) PHP part-3 - Students Founder€¦ · PHP MySQL Introduction MySQL is a database. The data in MySQL is stored in database objects called tables. A table

Jun 06, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Web Engineering (Lecture 11) PHP part-3 - Students Founder€¦ · PHP MySQL Introduction MySQL is a database. The data in MySQL is stored in database objects called tables. A table

Web Engineering

(Lecture 11)

PHP

part-3

By:

Mr. Sadiq Shah

Lecturer (CS)

Class: BS(IT)-6th semester

Page 2: Web Engineering (Lecture 11) PHP part-3 - Students Founder€¦ · PHP MySQL Introduction MySQL is a database. The data in MySQL is stored in database objects called tables. A table

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".

Page 3: Web Engineering (Lecture 11) PHP part-3 - Students Founder€¦ · PHP MySQL Introduction MySQL is a database. The data in MySQL is stored in database objects called tables. A table

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

Page 4: Web Engineering (Lecture 11) PHP part-3 - Students Founder€¦ · PHP MySQL Introduction MySQL is a database. The data in MySQL is stored in database objects called tables. A table

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

Page 5: Web Engineering (Lecture 11) PHP part-3 - Students Founder€¦ · PHP MySQL Introduction MySQL is a database. The data in MySQL is stored in database objects called tables. A table

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

Page 6: Web Engineering (Lecture 11) PHP part-3 - Students Founder€¦ · PHP MySQL Introduction MySQL is a database. The data in MySQL is stored in database objects called tables. A table

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.

Page 7: Web Engineering (Lecture 11) PHP part-3 - Students Founder€¦ · PHP MySQL Introduction MySQL is a database. The data in MySQL is stored in database objects called tables. A table

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.

Page 8: Web Engineering (Lecture 11) PHP part-3 - Students Founder€¦ · PHP MySQL Introduction MySQL is a database. The data in MySQL is stored in database objects called tables. A table

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.

Page 9: Web Engineering (Lecture 11) PHP part-3 - Students Founder€¦ · PHP MySQL Introduction MySQL is a database. The data in MySQL is stored in database objects called tables. A table

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), .... )

Page 10: Web Engineering (Lecture 11) PHP part-3 - Students Founder€¦ · PHP MySQL Introduction MySQL is a database. The data in MySQL is stored in database objects called tables. A table

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))“);

?>

Page 11: Web Engineering (Lecture 11) PHP part-3 - Students Founder€¦ · PHP MySQL Introduction MySQL is a database. The data in MySQL is stored in database objects called tables. A table

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.

Page 12: Web Engineering (Lecture 11) PHP part-3 - Students Founder€¦ · PHP MySQL Introduction MySQL is a database. The data in MySQL is stored in database objects called tables. A table

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)) “);

?>

Page 13: Web Engineering (Lecture 11) PHP part-3 - Students Founder€¦ · PHP MySQL Introduction MySQL is a database. The data in MySQL is stored in database objects called tables. A table

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,...)

Page 14: Web Engineering (Lecture 11) PHP part-3 - Students Founder€¦ · PHP MySQL Introduction MySQL is a database. The data in MySQL is stored in database objects called tables. A table

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)");

?>

Page 15: Web Engineering (Lecture 11) PHP part-3 - Students Founder€¦ · PHP MySQL Introduction MySQL is a database. The data in MySQL is stored in database objects called tables. A table

End of Lecture