0% found this document useful (0 votes)
58 views

PHP & Mysql: It84 - It Elective 1 (PHP Programming)

This document outlines the topics and objectives of an IT elective course on PHP programming and MySQL. The course will cover connecting to a MySQL database, performing queries to retrieve, insert, update, and delete data, and integrating MySQL with PHP scripts. Students will learn how to manipulate a MySQL database and develop dynamic websites or web applications using PHP and MySQL.

Uploaded by

WearIt Co.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

PHP & Mysql: It84 - It Elective 1 (PHP Programming)

This document outlines the topics and objectives of an IT elective course on PHP programming and MySQL. The course will cover connecting to a MySQL database, performing queries to retrieve, insert, update, and delete data, and integrating MySQL with PHP scripts. Students will learn how to manipulate a MySQL database and develop dynamic websites or web applications using PHP and MySQL.

Uploaded by

WearIt Co.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

IT84 – IT ELECTIVE 1 (PHP PROGRAMMING)

PHP & MySQL


PREPARED BY: JAY-AR GIWO BALAUAG
IT84 – IT ELECTIVE (PHP PROGRAMMING)

TOPICS
 MySql Introduction
 Connecting to Mysql
 MySql Error Handling
 Retrieving Data from a Database
 Inserting Data into a Database
 Deleting Data in a Database
 Updating Data in a Database
IT84 – IT ELECTIVE (PHP PROGRAMMING)

OBJECTIVES
At the end of the lesson, the students should be able to:
 Explain the concepts of MySQL
 Determine the function that is used in handling MySQL related
errors
 Manipulate MySQL database using the command update,
insert, delete and select.
 Integrate MySQL in PHP scripts
 Develop a dynamic website or web applications
IT84 – IT ELECTIVE (PHP PROGRAMMING)

MYSQL INTRODUCTION
MySQL is the most popular database system used with
PHP.
MySQL is a database system used on the web
MySQL is a database system that runs on a server
MySQL is ideal for both small and large applications
MySQL is very fast, reliable, and easy to use
IT84 – IT ELECTIVE (PHP PROGRAMMING)

MYSQL INTRODUCTION
MySQL supports standard SQL
MySQL compiles on a number of platforms
MySQL is free to download and use
MySQL is developed, distributed, and supported by
Oracle Corporation
MySQL is named after co-founder Monty Widenius's
daughter: My
IT84 – IT ELECTIVE (PHP PROGRAMMING)

CONNECTING TO MYSQL
Use the PHP mysqli_connect() function to open a new
connection to the MySQL server.
Syntax:
mysqli_connect(host, username, password, dbname);
Parameters Description
Host Optional. Either a host name or an IP address
Username Optional. The MySQL user name
Password Optional. The password to log in with
dbname Optional. The default database to be used when performing queries
IT84 – IT ELECTIVE (PHP PROGRAMMING)

CONNECTING TO MYSQL
Sample Code:
<?php
// Create connection

$con=mysqli_connect(“localhost", “root", “john2014", “profile");


// Check connection

if ($con) { echo "Failed to connect to MySQL: " .


mysqli_connect_error();
}
?>
IT84 – IT ELECTIVE (PHP PROGRAMMING)

RETRIEVING DATA FROM A DATABASE


The SELECT statement is used to select data from a database.

Syntax:
SELECT column_name(s) FROM table_name

mysqli_query() performs a query against the database. This


function will execute the SQL statement above.
Syntax:
mysqli_query(connection, SQL statement);
IT84 – IT ELECTIVE (PHP PROGRAMMING)

RETRIEVING DATA FROM A DATABASE


Sample database table (tbl_student)
SN Name
270093 Dela Cruz, Juan
210009 James, Mark

Sample code:
$result = mysqli_query($con, "SELECT * FROM Persons");
while($row = mysqli_fetch_array($result)) {
echo $row[0]. " " . $row[1]; echo "<br>";
}
Output: 270093 Dela Cruz, Juan
210009 James, Mark
IT84 – IT ELECTIVE (PHP PROGRAMMING)

DISPLAY THE RESULT IN AN HTML TABLE


$result = mysqli_query($con, "SELECT * FROM tbl_student");
echo "<table border='1'>
<tr>
<td>SN</td> <td>Name</td>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
   echo "<td>" . $row[0] . "</td>";
   echo "<td>" . $row[1] . "</td>";
echo "</tr>";
}
echo "</table>"; Output: SN Name
270093 Dela Cruz, Juan
210009 James, Mark
IT84 – IT ELECTIVE (PHP PROGRAMMING)

INSERTING DATA INTO A DATABASE


The INSERT INTO statement is used to add new records to a
database table.
Syntax:
INSERT INTO table_name VALUES (value1, value2, value3,...)

Sample Code:
mysqli_query($con, "INSERT INTO tbl_student (sn, name)
VALUES (‘230087', ‘Doe John’)");
Output: SN Name
270093 Dela Cruz, Juan
210009 James, Mark
230087 Doe, John
IT84 – IT ELECTIVE (PHP PROGRAMMING)

INSERT DATA FROM A FORM INTO A DATABASE


HTML Form:
<html>
<body>
<form action="insert.php" method="post">
SN: <input type="text" name=“sn"><br>
Name: <input type="text" name="name"><br>
<input type="submit">
</form>
</body>
</html>
Output:
IT84 – IT ELECTIVE (PHP PROGRAMMING)

INSERT DATA FROM A FORM INTO A DATABASE


<?php

$sn = $_POST[‘sn'];
$name = $_POST['name'];

$result= mysqli_query($con, “INSERT INTO tbl_student(sn, name)


VALUES ('$sn', '$name')”);

if(!$result) { die('Error: ' . mysqli_error()); }


else { echo "1 record added"; }
?>
IT84 – IT ELECTIVE (PHP PROGRAMMING)

DELETING DATA IN A DATABASE


The DELETE FROM statement is used to delete records from a
database table.
Syntax:
DELETE FROM table_name WHERE some_column = some_value

Sample code:
mysqli_query($con, "DELETE FROM tbl_student WHERE
sn='230087’");
IT84 – IT ELECTIVE (PHP PROGRAMMING)

UPDATING DATA IN A DATABASE


The UPDATE statement is used to modify data in a table.

Syntax:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value

Note: The where clause specifies which data will be updated. If you


remove the where clause, all records will be updated.
IT84 – IT ELECTIVE (PHP PROGRAMMING)

UPDATING DATA IN A DATABASE


Example table:
SN Name
270093 Dela Cruz, Juan
210009 James, Mark

Example code:
mysqli_query($con, "UPDATE tbl_student SET name= ‘Cruz,
John’ WHERE sn=‘270093’");
Updated record:
SN Name
270093 Cruz, Juan
210009 James, Mark
IT84 – IT ELECTIVE (PHP PROGRAMMING)

Questions?

You might also like