This document provides an overview of basic MySQL concepts and commands for working with databases in PHP, including connecting to a database, creating databases and tables, inserting, selecting, updating and deleting data, and using where clauses. Key points covered include what a database is, benefits over files, what MySQL is and how it works, connecting and selecting databases in PHP code, writing queries, and common SQL commands like create, insert, select, update and delete.
2. Files
Example
INPUT :
Price and product changes
OUTPUT :
Catalogue
Catalogue
File
Record Format :
(Product_no, Description, Price)
$file=fopen("welcome.txt","r");
fclose($file)
feof($file)
fgets($file) fgetc($file)
By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh
3. What is Database?
A database is an organized collection of data. The data are typically organized to model
relevant aspects of reality in a way that supports processes requiring this information
By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh
4. Database Vs Files
Files problems
• Have to write procedures to manage the accessibility of
common files.
• Errors occurs through using the files.
• Editing common files structure require editing all the
programs that deal with this file.
By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh
5. • 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
Intro
By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh
6. • What is MySQL?
MySQL supports 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
Intro
By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh
7. • Connect to DB:
mysql_connect('localhost',‘db_user',‘db_pass')
• Verifying and Select DB:
mysql_select_db(‘db_name')
MySQL
Configuration
By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh
8. • Why ?
To insert the content of one PHP file into another
PHP file before the server executes it.
• Syntax:
Include & Require
include 'filename';
or
require 'filename';
By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh
9. • What is Query ?
A query is a question or a request.
We can query a database for specific information and
have a record set returned.
• Ex:
SELECT LastName FROM Employees
MySQL
Query
By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh
10. • How can I write Query in php ?
$result = mysql_query("SELECT * FROM Persons");
• How can I get the results ?
mysql_fetch_array($result);
mysql_fetch_assoc($result);
MySQL
Query with php
By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh
11. MySQL
Create new DB
// Create database
$sql="CREATE DATABASE my_db";
if (mysql_query($sql)) {
echo "Database my_db created successfully";
} else {
echo "Error creating database: " . mysqli_error($con);
}
OR
12. MySQL
Create new Table
// Create table
$sql="CREATE TABLE Persons(FirstName CHAR(30),LastName
CHAR(30),Age INT)";
if (mysql_query($sql)) {
echo "Table persons created successfully";
} else {
echo "Error creating table: " . mysqli_error($con);
}
OR
15. MySQL
Insert Date to a Table
By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin',35)");
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Glenn', 'Quagmire',33)");
OR
16. MySQL
Select Data from Table
By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh
$result = mysql_query("SELECT * FROM Persons");
while($row = mysql_fetch_array($result)) {
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br>";
}
17. MySQL
Where clause
By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh
$result = mysql_query("SELECT * FROM Persons
WHERE FirstName='Peter'");
while($row = mysql_fetch_array($result)) {
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br>";
}
18. MySQL
Update Data In a Database
By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh
mysql_query("UPDATE Persons SET Age=36
WHERE FirstName='Peter' AND LastName='Griffin'");
19. MySQL
Delete Data In a Database
By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh
mysql_query(“DELETE FROM Persons
WHERE FirstName='Peter' AND LastName='Griffin'");