0% found this document useful (0 votes)
7 views15 pages

php4

MySQL is a fast, reliable, and easy-to-use database system ideal for web applications, developed by Oracle Corporation. The document provides an overview of how to connect PHP to MySQL, create databases and tables, insert, select, delete, and update data, as well as using the LIMIT clause for data selection. It includes code examples for each operation to demonstrate the usage of MySQL with PHP.

Uploaded by

bassel.2123018
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
7 views15 pages

php4

MySQL is a fast, reliable, and easy-to-use database system ideal for web applications, developed by Oracle Corporation. The document provides an overview of how to connect PHP to MySQL, create databases and tables, insert, select, delete, and update data, as well as using the LIMIT clause for data selection. It includes code examples for each operation to demonstrate the usage of MySQL with PHP.

Uploaded by

bassel.2123018
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

PHP MYSQL DATABASE

Faisal Salaheldeen
STEM Kalubya School
What is MySQL?
• 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
• MySQL uses 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
• The data in a MySQL database are stored in tables. A
table is a collection of related data, and it consists of
columns and rows.

• PHP Connect to MySQL

• PHP 5 and later can work with a MySQL database using:


• MySQLi extension (the "i" stands for improved)
• PDO (PHP Data Objects)
Open a Connection to MySQL
<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Close the Connection
• The connection will be closed automatically when the
script ends. To close the connection before, use the
following:
• mysqli_close($conn);
CREATE DATABASE
• The CREATE DATABASE statement is used to create a
database in MySQL.
• The following examples create a database named
"myDB"

CREATE DATABASE
<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// Create database
$sql = "CREATE DATABASE myDB";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}

mysqli_close($conn);
?>
PHP MySQL Create Table
• We will create a table named “students", with four columns:
"id", "firstname", "lastname" and "email”.

• <?php
// sql to create table
$sql = "CREATE TABLE students (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50)
)";

?>
PHP MySQL Insert Data
• After a database and a table have been created, we can
start adding data in them.
• Here are some syntax rules to follow:
• The SQL query must be quoted in PHP
• String values inside the SQL query must be quoted
• Numeric values must not be quoted
• The word NULL must not be quoted
PHP MySQL Insert Data
<?php

$sql = mysqli_query($conn, "INSERT INTO students (firstname,


lastname, email)
VALUES ('John', 'Doe', 'john@example.com')“ );
if ($sql) {
echo "New record created successfully";
//if you want to get last row inserted id use next function
$last_id = mysqli_insert_id($conn);
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
PHP MySQL select data
Example:
<?php
$sql = "SELECT id, firstname, lastname FROM students WHERE
lastname='Doe„ ORDER BY lastname";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " .
$row["lastname"]. "<br>";
}
} else {
echo "0 results";
}

mysqli_close($conn);
?>
PHP MySQL Delete Data
The DELETE statement is used to delete records from a table.
Example
<?php
// sql to delete a record
$sql = "DELETE FROM students WHERE id=3";

if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>
PHP MySQL Update Data
Example

<?php
$sql = "UPDATE students SET lastname='Doe' WHERE
id=2";

if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>
PHP MySQL Limit Data Selections
MySQL provides a LIMIT clause that is used to specify the
number of records to return.
Example
Assume we wish to select all records from 1 - 30 from a
table called "Orders". The SQL query would then look like
this:
$sql = "SELECT * FROM Orders LIMIT 30";

to return only 10 records, from the record after record15


$sql = "SELECT * FROM Orders LIMIT 15, 10";
PHP Includes
To include data from external php file inside current file use
the next clause.

<?php
Include(“php_file_name.php”);
?>

You might also like