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

40 MySQL DataBase.pptx

The document provides an overview of how to connect PHP to a MySQL database using MySQLi and PDO, with examples of both object-oriented and procedural approaches. It includes instructions for creating a database, creating a table, and performing CRUD operations (Create, Read, Update, Delete) on the database. Code snippets demonstrate the connection process, error handling, and executing SQL queries in PHP.

Uploaded by

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

40 MySQL DataBase.pptx

The document provides an overview of how to connect PHP to a MySQL database using MySQLi and PDO, with examples of both object-oriented and procedural approaches. It includes instructions for creating a database, creating a table, and performing CRUD operations (Create, Read, Update, Delete) on the database. Code snippets demonstrate the connection process, error handling, and executing SQL queries in PHP.

Uploaded by

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

PHP - MySql

Web Technologies
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)
• PDO will work on 12 different database systems, whereas MySQLi will
only work with MySQL databases.
• Both are object-oriented, but MySQLi also offers a procedural API.
Example (MySQLi Object-Oriented)
• <?php
$servername = "localhost";
$username = "username";
$password = "password";

• // Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Example (MySQLi Procedural)

• <?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
• MySQLi Object-Oriented:
• $conn->close();

• MySQLi Procedural:
• mysqli_close($conn);
PHP Create a MySQL 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);
?>
Create a table in MySql and inserting
• 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
)
• INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)


VALUES ('John', 'Doe', '[email protected]')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
PHP MySQL Update Data

• UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
Example (MySQLi Procedural)
• <?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "UPDATE MyGuests 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 Delete Data
• Delete Data From a MySQL Table Using MySQLi
• DELETE FROM table_name
WHERE some_column = some_value
• <?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// sql to delete a record
$sql = "DELETE FROM MyGuests WHERE id=3";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>

You might also like