php4
php4
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.
// 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
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";
<?php
Include(“php_file_name.php”);
?>