Top Banner
Web Basics Programming With PHP
20

Php workshop L04 database

Jul 15, 2015

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Php workshop L04 database

Web Basics Programming With PHP

Page 2: Php workshop L04 database

FilesExample

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

Page 3: Php workshop L04 database

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

Page 4: Php workshop L04 database

Database Vs FilesFiles 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

Page 5: Php workshop L04 database

• 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

MySQLIntro

By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh

Page 6: Php workshop L04 database

• 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

MySQLIntro

By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh

Page 7: Php workshop L04 database

• Connect to DB:

mysql_connect('localhost',‘db_user',‘db_pass')

• Verifying and Select DB:

mysql_select_db(‘db_name')

MySQLConfiguration

By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh

Page 8: Php workshop L04 database

• 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

Page 9: Php workshop L04 database

• 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

MySQLQuery

By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh

Page 10: Php workshop L04 database

• 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);

MySQLQuery with php

By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh

Page 11: Php workshop L04 database

MySQLCreate 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

Page 12: Php workshop L04 database

MySQLCreate new Table

// Create table$sql="CREATE TABLE Persons(FirstName CHAR(30),LastNameCHAR(30),Age INT)";if (mysql_query($sql)) {

echo "Table persons created successfully";} else {

echo "Error creating table: " . mysqli_error($con);}

OR

Page 13: Php workshop L04 database

MySQLCreate new Table

By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh

Page 14: Php workshop L04 database

MySQLInsert Date to a Table

By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh

Page 15: Php workshop L04 database

MySQLInsert 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

Page 16: Php workshop L04 database

MySQLSelect 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>";

}

Page 17: Php workshop L04 database

MySQLWhere clause

By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh

$result = mysql_query("SELECT * FROM PersonsWHERE FirstName='Peter'");

while($row = mysql_fetch_array($result)) {echo $row['FirstName'] . " " . $row['LastName'];echo "<br>";

}

Page 18: Php workshop L04 database

MySQLUpdate Data In a Database

By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh

mysql_query("UPDATE Persons SET Age=36WHERE FirstName='Peter' AND LastName='Griffin'");

Page 19: Php workshop L04 database

MySQLDelete Data In a Database

By: Mohammad Al-Shalabi & Mohammad Opada Al-Bosh

mysql_query(“DELETE FROM PersonsWHERE FirstName='Peter' AND LastName='Griffin'");

Page 20: Php workshop L04 database

THANK YOU!

Your Logo