Professional Documents
Culture Documents
ITC-226 Web Systems & Tech: PHP and Mysql
ITC-226 Web Systems & Tech: PHP and Mysql
1
Course Teacher
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
6
SQL SELECT (Read)
Syntax
7
SQL UPDATE (Update)
SYNTAX
UPDATE table
SET column1 = ‘some_text’
WHERE id = 1;
8
SQL DELETE (Delete)
SYNTAX
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);
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
• http://localhost/phpmyadmin
13
Using PHPMyAdmin
• It enables you to access your MySQL database through a
GUI. You can easily do the following:
15
COMMONLY USED
FUNCTIONS
• mysqli_connect(“hostname”,”user”,”pass”,“dbname”)
;
• mysqli_query(handle, 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.
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