0% found this document useful (0 votes)
5 views3 pages

PHP Mysql - Summary

The document provides a step-by-step guide on how to interact with a MySQL database using PHP. It covers opening a connection, creating a database and table, inserting, selecting, filtering, updating, and deleting data. Each section includes code snippets for implementation and error handling.

Uploaded by

ariraghad
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)
5 views3 pages

PHP Mysql - Summary

The document provides a step-by-step guide on how to interact with a MySQL database using PHP. It covers opening a connection, creating a database and table, inserting, selecting, filtering, updating, and deleting data. Each section includes code snippets for implementation and error handling.

Uploaded by

ariraghad
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/ 3

PHP MySQL Database

1. Open a Connection to MySQL Server


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

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

// Check connection
if (!$conn) {
die("Connection failed: " . mysql_connect_error());
}
echo "Connected successfully";
?>

2. Create a MySQL Database (myDB)


// Create database

$sql = "CREATE DATABASE myDB";

if (mysql_query($conn, $sql)==True) {
echo "Database created successfully";
} else {
echo "Error creating database: " ;
}
3. Create a MySQL Table
// sql to create table
$sql = "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
)";

if (mysql_query($conn, $sql)==False) {
echo " Error creating table: ";
} else {
echo " Table MyGuests created successfully " ;
}

4. PHP MySQL Insert Data


$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Hiba', 'Salah', '[email protected]')";

if (mysql_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: in executing the inserting query" ;
}

5. Select Data From a MySQL Database


$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysql_query($conn, $sql);

if (mysql_num_rows($result) > 0) {
// output data of each row
while($row = mysql_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " .
$row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
6. Select and Filter Data From a MySQL
Database
$sql = "SELECT id, firstname, lastname FROM MyGuests WHERE
lastname='Ahmad'";
$result = mysql_query($conn, $sql);

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

7. Delete Data From a MySQL Table


// sql to delete a record
$sql = "DELETE FROM MyGuests WHERE id=3";

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

8. Update Data In a MySQL Table


$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

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

You might also like