Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

ITC-226

Web Systems & Tech


Lecture 10
PHP and mysql

1
Course Teacher

• Jawad Ahmed Bhutta


• https://sites.google.com/site/csjawadbhutta/
web-engineering-spring2018

• Cubicle 2 BIC Room Iqbal Block BUKC

2
Lecture Outline

3
INTRODUCTION
• In order to have really full featured application. We
are going to need to incorporate a database.
• We can store lots of information in database,
search through it.
• Added it, updated. Keep it there for long period of
time.
• There are so many benefits of using database.

4
CRUD
• CRUD is acronym and it stands for…
CRUD
Create, Read, Update, Delete
These are the basic operations perform on the data
base.
• We create records in the database.
• We read records back in the database.
• We update records in the database.
• We completely delete records in the database.
5
SQL INSERT (Create)
SYNTAX

INSERT INTO table (column1, column2,


column3) VALUES (val1, val2, val3);

6
SQL SELECT (Read)
Syntax

SELECT * FROM table


WHERE column1 = ‘some_text’
ORDER BY column1, column2 ASC;

7
SQL UPDATE (Update)
SYNTAX

UPDATE table
SET column1 = ‘some_text’
WHERE id = 1;

8
SQL DELETE (Delete)
SYNTAX

DELETE FROM table WHERE id = 1;

9
CREATE COMMAND
CREATE DATABASE University;
USE University;
CREATE TABLE subjects (
id int(11) NOT NULL auto_increment,
menu_name varchar(30) NOT NULL,
position int(3) NOT NULL,
visible tinyint(1) NOT NULL,
PRIMARY KEY (id)
);

10
SQL COMMAND
• INSERT INTO subjects (menu_name, position,
visible) VALUES (‘About Widget Corp’, 1, 1);

• SELECT * FROM subjects;

• INSERT INTO subjects (menu_name, position,


visible) VALUES (‘Products’, 2, 1);
• INSERT INTO subjects (menu_name, position,
visible) VALUES (‘Services’, 3, 1);
• INSERT INTO subjects (menu_name, position,
visible) VALUES (‘Misc’, 4, 0);

11
SQL COMMAND
• SELECT * FROM subjects
WHERE visible = 1
ORDER BY position ASC/DESC;
• SELECT id, menu_name FROM subjects
WHERE visible = 1
ORDER BY position ASC/DESC;
• UPDATE subjects
SET visible = 1
WHERE id = 4;

12
Php MyAdmin
• www.phpmyadmin.net

• It is already included in WAMP/XAMP one package.

• http://localhost/phpmyadmin

13
Using PHPMyAdmin
• It enables you to access your MySQL database through a
GUI. You can easily do the following:

• Drop and create databases


• Create, edit and delete tables
• Create, edit and delete fields
• Enter any MySQL statements
• View and Print table structure
• Generate PHP code
• View data in table format
14
PHP Database Interaction in FOUR
steps
• Create a database connection
• Perform database query
• Use returned data (if any)
• Close connection

15
COMMONLY USED
FUNCTIONS
• mysqli_connect(“hostname”,”user”,”pass”,“dbname”)
;

• mysqli_query(handle, query”);

• mysqli_num_rows(result variable from query);

• mysqli_fetch_array(“result variable from query”);

• mysqli_fetch_assoc(“result variable from query”)

• mysqli_error();

• md5(string);
16
CONNECTING TO A
DATABASE
• STEP 1: create connection
<?php
$connection = mysqli_connect(“localhost”, “root”, “password”,
“University”);
if (!$connection) {
die(“Database connection failed: ” . mysqli_error());
}
?>
<html>

</html>
• this function returns a value, that will stored in $connection.
That value is called handle. 17
CONNECTING TO A
DATABASE
• STEP 2: perform database query
<html>
<head></head>
<body>
<?php
$query = “SELECT * FROM subjects”;
$result = mysqli_query($connection, $query);
if (!$result) {
die(“Database query failed: ” .
mysqli_error());
}
?>
</body>
</html>
18
CONNECTING TO A
DATABASE
• STEP 3: using returned data
<html> ...
<?php
// 3. Use returned data
while ($row = mysqli_fetch_array($result))
{
echo $row[1] . “ ” . $row[2] . “<br />”;
}
?>
</html>
19
CONNECTING TO A
DATABASE
• Step 4: closing connection
Step 1
<html>
<head></head>
<body>
Step 2
Step 3
</body>
</html>
<?php
// 5. Close connection
mysqli_close($connection);
?> 20
EXAMPLE
• Create a database name school in MySQL.

• Create a table name result, with following fields sId,


sName and Marks.

• Create a file name connection.php

21
EXAMPLE
Create connection
<?php
// 1. create a connection
$connection =
mysqli_connect(“localhost”,”root”,””,”scho
ol”);
if(!connection) {
die(“database connection failed” .
mysqli_error());
}
?>
22
EXAMPLE
Step 1 (create a database connection)
<html>
<head></head>
<body>
<?php
// 2. Perform database query
$query = “SELECT * FROM result”;
$result = mysqli_query($connection, $query);
if (!$result) {
die(“Database query failed: ” . mysqli_error());
}
if(mysqli_num_rows($result <= 0)){
die(“No record found”);
}
?>
</body>
</html>
23
EXAMPLE
Step 1 (create a database connection)
<html>…
<?php
Step 2 (perform database query)
// 3. Use returned data
while ($row =
mysqli_fetch_array($result)) {
echo $row[0] . “<br />”;
echo $row[1] . “<br />”;
echo $row[2] . “<br />”;
}
?>
</html> //step 4
<?php mysqli_close($connection) ?> 24
Connection.php

25
Update_record.php

26
HTML FORM

27
Update_record.php

28
29

You might also like