0% found this document useful (0 votes)
11 views13 pages

Lesson 6 PHP MYSQL CRUD

This lesson covers PHP-MySQL CRUD operations, including how to connect to a MySQL database and perform SELECT, INSERT, UPDATE, and DELETE statements. It explains the structure of a MySQL database, the creation of tables, and the importance of primary keys. Additionally, it provides code examples for each operation using the MySQLi extension.

Uploaded by

owekesa361
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

Lesson 6 PHP MYSQL CRUD

This lesson covers PHP-MySQL CRUD operations, including how to connect to a MySQL database and perform SELECT, INSERT, UPDATE, and DELETE statements. It explains the structure of a MySQL database, the creation of tables, and the importance of primary keys. Additionally, it provides code examples for each operation using the MySQLi extension.

Uploaded by

owekesa361
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Lesson 5

PHP-MYSQL
CRUD

1
Lesson Learning
Outcomes
• At the end of this Topic, the learner
should be able to:

i. Write a PHP-MYSQL connection string


ii. Write PHP-MYSQL SELECT Statements
iii. Write PHP-MYSQL INSERT Statements
iv. Write PHP-MYSQL UPDATE
Statements
v. Write PHP-MYSQL DELETE
Statements
2
PHP MySQL Database
With PHP, you can connect to and
manipulate databases.
MySQL is the most popular database
system used with PHP.
MySQL uses standard SQL and runs on a
server
MySQL is developed, distributed, and
supported by Oracle Corporation.
The data in a MySQL database are stored
in tables consisting of a collection of
related data, and it consists of columns
and rows. 3
Database Queries
A query is a question or a request
and we can query a database for
specific information and have a
recordset returned.

Example:

SELECT LastName FROM Employees


The query above selects all the data
in the "LastName" column from the
"Employees" table. 4
Creating a MySQL Table
Using MySQLi
 The CREATE TABLE statement is used to create a
table in MySQL.

 We will create a table named "MyGuests", with five


columns: "id", "firstname", "lastname", "email" and
"reg_date":

 CREATE TABLE MyGuests (


 id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 firstname VARCHAR(30) NOT NULL,
 lastname VARCHAR(30) NOT NULL,
 email VARCHAR(50),
 reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP
)
5
Notes on MYSQL Table
 NOT NULL - Each row must contain a value for that
column, null values are not allowed
 DEFAULT value - Set a default value that is added
when no other value is passed
 UNSIGNED - Used for number types, limits the stored
data to positive numbers and zero
 AUTO INCREMENT - MySQL automatically increases
the value of the field by 1 each time a new record is
added
 PRIMARY KEY - Used to uniquely identify the rows in
a table.
 The column with PRIMARY KEY setting is often an ID
number, and is often used with AUTO_INCREMENT
 Each table should have a primary key column (in this
case: the "id" column). Its value must be unique for
each record in the table.
6
PHP-MySQL Connection Types

PHP 5 and later can work with a MySQL database


using:

MySQLi extension (the "i" stands for improved)


PDO (PHP Data Objects)

Earlier versions of PHP used the MySQL extension.

However, this extension was deprecated in 2012.

7
1.PHP-MySQL Connection String

1. <?php
2. $servername = "localhost";
3. $username = "username";
4. $password = "password";

5. // Create connection
6. $conn = new mysqli($servername, $username,
$password);

7. // Check connection
8. if ($conn->connect_error) {
9. die("Connection failed: " . $conn->connect_error);
10.}
11.echo "Connected successfully";
12.?>
8
2.PHP-MySQL Insert Data
1. $sql = "INSERT INTO MyGuests
(firstname, lastname, email)
2. VALUES ('John', 'Doe',
'john@example.com')";

3. if ($conn->query($sql) === TRUE) {


4. echo "New record created successfully";
5. } else {
6. echo "Error: " . $sql . "<br>" . $conn-
>error;
7. }

8. $conn->close(); 9
3.PHP-MySQL Select Data
1. $sql = "SELECT id, firstname, lastname
FROM MyGuests";
2. $result = $conn->query($sql);
3. if ($result->num_rows > 0) {
4. // output data of each row
5. while($row = $result->fetch_assoc()) {
6. echo "id: " . $row["id"]. " - Name: " .
$row["firstname"]. " " . $row["lastname"].
"<br>";
7. }
8. } else {
9. echo "0 results";
10.}
11.$conn->close(); 10
4.PHP MySQL Update Data
1. $sql = "UPDATE MyGuests SET
lastname='Doe' WHERE id=2";

2. if ($conn->query($sql) === TRUE) {


3. echo "Record updated successfully";
4. } else {
5. echo "Error updating record: " . $conn-
>error;
6. }

7. $conn->close();
8. ?> 11
5. PHP MySQL Delete
1.// sql to delete aData
record
2. $sql = "DELETE FROM MyGuests WHERE
id=3";

3. if ($conn->query($sql) === TRUE) {


4. echo "Record deleted successfully";
5. } else {
6. echo "Error deleting record: " . $conn-
>error;
7. }

8. $conn->close();
9. ?> 12
The end
Thank you
04/21/2025 13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy