SlideShare uma empresa Scribd logo
1 de 26
PHP
Basic SQL
definition: Database
• A database which structures data in the form
of tables. Each table contains information
relevant to a particular feature, and is linked
to other tables by a common value. For
example, two attribute tables could be linked
to a spatial data table via a Geocode, such as
the postcode.
A very short explanation
• In one database. There are many tables.
• One table represents a set of entity. For example ‘People’ ,
‘Cars’ , ‘Movies’
• Table has many rows. One row in table represents instance of
entities. For example Toni Meechai’ , ‘Kobota Sedan’ ,
‘Twilight’
• Table also has many column. One column represents property
of an instance. For example Nationality’ , ‘How fast’ , ‘How
boring it is’
• When you want get or do something with Database Tables,
you can query it like ‘Please show me all people’.
Unfortunately, you have to follow SQL format.
Play with PHP MyAdmin
• Pay attention to demonstration.
• Try to understand ideas of ‘Database’ , ‘Table’,
‘Row’, ‘Basic usage of SQL’.
• Learners can follow this article:
http://www.php-editors.com/articles/sql_phpmyadmin.php
SQL : Selecting data
Selecting from 1 table
• SELECT * FROM COUNTRY;
• SELECT * FROM COUNTRY
WHERE CONTINECT=‘ASIA’;
• SELECT CODE,NAME,CONTINENT,POPULATION
FROM COUNTRY
WHERE CONTINENT=‘ASIA’;
• SELECT NAME,POPULATION
FROM COUNTRY
WHERE CONTINENT=‘ASIA’
ORDER BY POPULATION DESC;
• If we want to reverse it, change DESC to ASC
instead
• If it has many rows, we want just 10 rows
• SELECT NAME,POPULATION
FROM COUNTRY
WHERE CONTINENT=‘ASIA’
ORDER BY POPULATION DESC
LIMIT 10;
• If we want to change column name
• SELECT NAME AS “ประเทศ สุ่ม”
FROM COUNTRY
WHERE CONTINENT=‘ASIA’
ORDER BY RAND()
LIMIT 10;
• NOTE, If you want to change name to be
something like ประเทศ สุ่ม, it will throws an
error
• Because that word contains whitespace.
• List all countries started with ‘t’
SELECT NAME FROM COUNTRY
WHERE NAME LIKE ‘T%’;
• List all countries ended with ‘land’
SELECT NAME FROM COUNTRY
WHERE NAME LIKE ‘%land’;
• List all countries contains ‘ko’
SELECT NAME FROM COUNTRY
WHERE NAME LIKE ‘%ko%’;
Tables joining
Why joining?
• Refer to database course. It is better to store
different content into different table.
Reducing data redundant.
• MySQL, a database server we use is a
Relational engine. We can say one table can
has relationship to other tables.
• To get data across tables, we have to do
joining selection.
Relationship in our 3 tables
• Consider our 3 tables: Country, City,
CountryLanguage
• Table: Country has column ‘Code’
Table: City has column ‘CountryCode’
Table: CountryLanguage has column
‘CountryCode’
• Country.Code, City.CountryCode,
CountryLanguage.CountryCode are sharing
relationship.
• List all city in Thailand
SELECT * FROM CITY,COUNTRY
WHERE CODE=‘THA’
AND CODE=COUNTRYCODE;
• Remember. To join 2 tables we have to do 2
things.
1)Include 2 tables’name in From clause
2)Match the related columns in each 2 tables by
using equality in where clause
• If we have same columns name in 2 tables,
you will get error cause there are ambiguous
column names.
• To fix this problem, specific table name before
column name by using .(dot)
SELECT CITY.NAME,COUNTRY.NAME
FROM CITY,COUNTRY
WHERE CODE=‘THA’
AND COUNTRY.CODE=CITY.COUNTRYCODE;
Count and Sum
• Find surface area of Thailand + Malaysia
• SELECT SUM(SURFACEAREA)
FROM COUNTRY
WHERE NAME=‘THAILAND’
OR NAME=‘MALAYSIA’;
• Find how many cities located in Asia
• SELECT COUNT(CITY.NAME)
FROM CITY,COUNTRY
WHERE CONTINENT=‘ASIA’
AND COUNTRY.CODE=CITY.COUNTRYCODE;
Changing Data Using SQL
INSERT / UPDATE / DELETE
Prepare table first.
CREATE TABLE Student (
id bigint(20) auto_increment PRIMARY KEY,
email varchar(255),
firstname varchar(255),
lastname varchar(255),
nick varchar(255)
);
Inserting
• — 1. Use SQL: “DESCRIBE tablename” to see
the target table’s structure.
Remember column name, type and sequence
• 2. Use SQL: “INSERT INTO
VALUES(xxx,yyy,zzz)” Replace xxx,yyy,zzz with
actual values you want to insert ordering as
the table column’s sequence.
INSERT INTO
student
values(
1,
‘taw@narak.com’,
‘Taw’,
‘Poldee’,
‘Tawny’
);"
Updating
• — Updating SQL syntax is
UPDATE tablename
SET columnanme = newvalue
WHERE condition
UPDATE student
SET nick=‘Tawjung’
WHERE id=1;
Deleting
• Deleting SQL syntax is
DELETE FROM tablename WHERE condition.
DELETE FROM student
WHERE firstname=‘Taw’;
PHP interface with MySQL
<?php
mysql_connect("localhost:portnum", "user","password") or
die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());
$result = mysql_query("SELECT column1,column2 FROM
tablename") or die(mysql_error());
while($row = mysql_fetch_array($result)) {
print $row['column1'];
print $row['column2'];
}
?>

Mais conteúdo relacionado

Mais procurados

SQL: Structured Query Language
SQL: Structured Query LanguageSQL: Structured Query Language
SQL: Structured Query LanguageRohit Bisht
 
DDL DATA DEFINATION LANGUAGE
DDL DATA DEFINATION LANGUAGEDDL DATA DEFINATION LANGUAGE
DDL DATA DEFINATION LANGUAGEAbrar ali
 
Sql a practical introduction
Sql   a practical introductionSql   a practical introduction
Sql a practical introductionHasan Kata
 
Mysql Crud, Php Mysql, php, sql
Mysql Crud, Php Mysql, php, sqlMysql Crud, Php Mysql, php, sql
Mysql Crud, Php Mysql, php, sqlAimal Miakhel
 
Structured query language(sql)ppt
Structured query language(sql)pptStructured query language(sql)ppt
Structured query language(sql)pptGowarthini
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQLEhsan Hamzei
 
SQL SERVER Training in Pune Slides
SQL SERVER Training in Pune SlidesSQL SERVER Training in Pune Slides
SQL SERVER Training in Pune Slidesenosislearningcom
 

Mais procurados (12)

SQL: Structured Query Language
SQL: Structured Query LanguageSQL: Structured Query Language
SQL: Structured Query Language
 
DDL DATA DEFINATION LANGUAGE
DDL DATA DEFINATION LANGUAGEDDL DATA DEFINATION LANGUAGE
DDL DATA DEFINATION LANGUAGE
 
Database Languges
Database LangugesDatabase Languges
Database Languges
 
Sql a practical introduction
Sql   a practical introductionSql   a practical introduction
Sql a practical introduction
 
Mysql Crud, Php Mysql, php, sql
Mysql Crud, Php Mysql, php, sqlMysql Crud, Php Mysql, php, sql
Mysql Crud, Php Mysql, php, sql
 
Structured query language(sql)ppt
Structured query language(sql)pptStructured query language(sql)ppt
Structured query language(sql)ppt
 
Sql Basics And Advanced
Sql Basics And AdvancedSql Basics And Advanced
Sql Basics And Advanced
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Html tables
Html tablesHtml tables
Html tables
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
SQL SERVER Training in Pune Slides
SQL SERVER Training in Pune SlidesSQL SERVER Training in Pune Slides
SQL SERVER Training in Pune Slides
 
INTRO TO SQL
INTRO TO SQLINTRO TO SQL
INTRO TO SQL
 

Destaque (7)

Sql ppt
Sql pptSql ppt
Sql ppt
 
History of computer
History of computerHistory of computer
History of computer
 
Lesson 1: Scratch Computer Programming
Lesson 1: Scratch Computer ProgrammingLesson 1: Scratch Computer Programming
Lesson 1: Scratch Computer Programming
 
Lang generations 7557_syed_ghazanfarnaqvi_saturday
Lang generations 7557_syed_ghazanfarnaqvi_saturdayLang generations 7557_syed_ghazanfarnaqvi_saturday
Lang generations 7557_syed_ghazanfarnaqvi_saturday
 
Les01 Writing Basic Sql Statements
Les01 Writing Basic Sql StatementsLes01 Writing Basic Sql Statements
Les01 Writing Basic Sql Statements
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
Unit1 principle of programming language
Unit1 principle of programming languageUnit1 principle of programming language
Unit1 principle of programming language
 

Semelhante a php basic sql

Php, mysq lpart5(mysql)
Php, mysq lpart5(mysql)Php, mysq lpart5(mysql)
Php, mysq lpart5(mysql)Subhasis Nayak
 
Introduction to SQL (for Chicago Booth MBA technology club)
Introduction to SQL (for Chicago Booth MBA technology club)Introduction to SQL (for Chicago Booth MBA technology club)
Introduction to SQL (for Chicago Booth MBA technology club)Jennifer Berk
 
Mangala Deshpande MySQL0710.ppt
Mangala Deshpande MySQL0710.pptMangala Deshpande MySQL0710.ppt
Mangala Deshpande MySQL0710.pptMihir Shah
 
Tk2323 lecture 7 sql
Tk2323 lecture 7   sql Tk2323 lecture 7   sql
Tk2323 lecture 7 sql MengChun Lam
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowPavithSingh
 
Advanced Database Systems - Presentation 3.pptx
Advanced Database Systems - Presentation 3.pptxAdvanced Database Systems - Presentation 3.pptx
Advanced Database Systems - Presentation 3.pptxEllenGracePorras
 
Intro To TSQL - Unit 1
Intro To TSQL - Unit 1Intro To TSQL - Unit 1
Intro To TSQL - Unit 1iccma
 
Intro to tsql unit 1
Intro to tsql   unit 1Intro to tsql   unit 1
Intro to tsql unit 1Syed Asrarali
 
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDSORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDSNewyorksys.com
 

Semelhante a php basic sql (20)

Php, mysq lpart5(mysql)
Php, mysq lpart5(mysql)Php, mysq lpart5(mysql)
Php, mysq lpart5(mysql)
 
Introduction to SQL (for Chicago Booth MBA technology club)
Introduction to SQL (for Chicago Booth MBA technology club)Introduction to SQL (for Chicago Booth MBA technology club)
Introduction to SQL (for Chicago Booth MBA technology club)
 
SQL
SQLSQL
SQL
 
Mangala Deshpande MySQL0710.ppt
Mangala Deshpande MySQL0710.pptMangala Deshpande MySQL0710.ppt
Mangala Deshpande MySQL0710.ppt
 
MySQL Essential Training
MySQL Essential TrainingMySQL Essential Training
MySQL Essential Training
 
Tk2323 lecture 7 sql
Tk2323 lecture 7   sql Tk2323 lecture 7   sql
Tk2323 lecture 7 sql
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
 
Advanced Database Systems - Presentation 3.pptx
Advanced Database Systems - Presentation 3.pptxAdvanced Database Systems - Presentation 3.pptx
Advanced Database Systems - Presentation 3.pptx
 
1.2 sql create and drop table
1.2 sql create and drop table1.2 sql create and drop table
1.2 sql create and drop table
 
Sql introduction
Sql introductionSql introduction
Sql introduction
 
SQL PPT.pptx
SQL PPT.pptxSQL PPT.pptx
SQL PPT.pptx
 
Sql
SqlSql
Sql
 
Intro To TSQL - Unit 1
Intro To TSQL - Unit 1Intro To TSQL - Unit 1
Intro To TSQL - Unit 1
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
 
Intro to tsql unit 1
Intro to tsql   unit 1Intro to tsql   unit 1
Intro to tsql unit 1
 
Database
Database Database
Database
 
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDSORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
SQL cheat sheet.pdf
SQL cheat sheet.pdfSQL cheat sheet.pdf
SQL cheat sheet.pdf
 
ORACLE PL SQL
ORACLE PL SQLORACLE PL SQL
ORACLE PL SQL
 

Mais de tumetr1

ตัวอย่างประวัติผู้วิจัย เล่มโปรเจ็ค
ตัวอย่างประวัติผู้วิจัย เล่มโปรเจ็คตัวอย่างประวัติผู้วิจัย เล่มโปรเจ็ค
ตัวอย่างประวัติผู้วิจัย เล่มโปรเจ็คtumetr1
 
ตัวอย่างภาคผนวก เล่มโปรเจ็ค
ตัวอย่างภาคผนวก เล่มโปรเจ็คตัวอย่างภาคผนวก เล่มโปรเจ็ค
ตัวอย่างภาคผนวก เล่มโปรเจ็คtumetr1
 
ตัวอย่างบรรณานุกรม เล่มโปรเจ็ค
ตัวอย่างบรรณานุกรม เล่มโปรเจ็คตัวอย่างบรรณานุกรม เล่มโปรเจ็ค
ตัวอย่างบรรณานุกรม เล่มโปรเจ็คtumetr1
 
ตัวอย่างบทที่1 บทนำ เล่มโปรเจ็ค
ตัวอย่างบทที่1 บทนำ เล่มโปรเจ็คตัวอย่างบทที่1 บทนำ เล่มโปรเจ็ค
ตัวอย่างบทที่1 บทนำ เล่มโปรเจ็คtumetr1
 
ตัวอย่างสารบัญ เล่มโปรเจ็ค
ตัวอย่างสารบัญ เล่มโปรเจ็คตัวอย่างสารบัญ เล่มโปรเจ็ค
ตัวอย่างสารบัญ เล่มโปรเจ็คtumetr1
 
ตัวอย่างกิตติกรรมประกาศ เล่มโปรเจ็ค
ตัวอย่างกิตติกรรมประกาศ เล่มโปรเจ็คตัวอย่างกิตติกรรมประกาศ เล่มโปรเจ็ค
ตัวอย่างกิตติกรรมประกาศ เล่มโปรเจ็คtumetr1
 
ตัวอย่างบทคัดย่อเล่มโปรเจ็ค
ตัวอย่างบทคัดย่อเล่มโปรเจ็คตัวอย่างบทคัดย่อเล่มโปรเจ็ค
ตัวอย่างบทคัดย่อเล่มโปรเจ็คtumetr1
 
file transfer and access utilities
file transfer and access utilitiesfile transfer and access utilities
file transfer and access utilitiestumetr1
 
retrieving the mail
retrieving the mailretrieving the mail
retrieving the mailtumetr1
 
connectivity utility
connectivity utilityconnectivity utility
connectivity utilitytumetr1
 
network hardware
network hardwarenetwork hardware
network hardwaretumetr1
 
ระบบเครือข่ายไร้สาย (wireless lan)
ระบบเครือข่ายไร้สาย (wireless lan)ระบบเครือข่ายไร้สาย (wireless lan)
ระบบเครือข่ายไร้สาย (wireless lan)tumetr1
 
the transport layer
the transport layerthe transport layer
the transport layertumetr1
 
ระดับชั้นเน็ตเวิร์ก
ระดับชั้นเน็ตเวิร์กระดับชั้นเน็ตเวิร์ก
ระดับชั้นเน็ตเวิร์กtumetr1
 
ระดับชั้นดาต้าลิงค์
ระดับชั้นดาต้าลิงค์ระดับชั้นดาต้าลิงค์
ระดับชั้นดาต้าลิงค์tumetr1
 
สถาปัตยกรรมเครือข่ายคอมพิวเตอร์และบริการ
สถาปัตยกรรมเครือข่ายคอมพิวเตอร์และบริการสถาปัตยกรรมเครือข่ายคอมพิวเตอร์และบริการ
สถาปัตยกรรมเครือข่ายคอมพิวเตอร์และบริการtumetr1
 
การส่งข้อมูลผ่านสายส่งและเทคนิคการส่งข้อมูลผ่านเครือข่าย
การส่งข้อมูลผ่านสายส่งและเทคนิคการส่งข้อมูลผ่านเครือข่ายการส่งข้อมูลผ่านสายส่งและเทคนิคการส่งข้อมูลผ่านเครือข่าย
การส่งข้อมูลผ่านสายส่งและเทคนิคการส่งข้อมูลผ่านเครือข่ายtumetr1
 
ความรู้พื้นฐานของระบบการสื่อสารข้อมูล
ความรู้พื้นฐานของระบบการสื่อสารข้อมูลความรู้พื้นฐานของระบบการสื่อสารข้อมูล
ความรู้พื้นฐานของระบบการสื่อสารข้อมูลtumetr1
 
พัฒนาเศรษฐกิจ
พัฒนาเศรษฐกิจพัฒนาเศรษฐกิจ
พัฒนาเศรษฐกิจtumetr1
 

Mais de tumetr1 (20)

ตัวอย่างประวัติผู้วิจัย เล่มโปรเจ็ค
ตัวอย่างประวัติผู้วิจัย เล่มโปรเจ็คตัวอย่างประวัติผู้วิจัย เล่มโปรเจ็ค
ตัวอย่างประวัติผู้วิจัย เล่มโปรเจ็ค
 
ตัวอย่างภาคผนวก เล่มโปรเจ็ค
ตัวอย่างภาคผนวก เล่มโปรเจ็คตัวอย่างภาคผนวก เล่มโปรเจ็ค
ตัวอย่างภาคผนวก เล่มโปรเจ็ค
 
ตัวอย่างบรรณานุกรม เล่มโปรเจ็ค
ตัวอย่างบรรณานุกรม เล่มโปรเจ็คตัวอย่างบรรณานุกรม เล่มโปรเจ็ค
ตัวอย่างบรรณานุกรม เล่มโปรเจ็ค
 
ตัวอย่างบทที่1 บทนำ เล่มโปรเจ็ค
ตัวอย่างบทที่1 บทนำ เล่มโปรเจ็คตัวอย่างบทที่1 บทนำ เล่มโปรเจ็ค
ตัวอย่างบทที่1 บทนำ เล่มโปรเจ็ค
 
ตัวอย่างสารบัญ เล่มโปรเจ็ค
ตัวอย่างสารบัญ เล่มโปรเจ็คตัวอย่างสารบัญ เล่มโปรเจ็ค
ตัวอย่างสารบัญ เล่มโปรเจ็ค
 
ตัวอย่างกิตติกรรมประกาศ เล่มโปรเจ็ค
ตัวอย่างกิตติกรรมประกาศ เล่มโปรเจ็คตัวอย่างกิตติกรรมประกาศ เล่มโปรเจ็ค
ตัวอย่างกิตติกรรมประกาศ เล่มโปรเจ็ค
 
ตัวอย่างบทคัดย่อเล่มโปรเจ็ค
ตัวอย่างบทคัดย่อเล่มโปรเจ็คตัวอย่างบทคัดย่อเล่มโปรเจ็ค
ตัวอย่างบทคัดย่อเล่มโปรเจ็ค
 
file transfer and access utilities
file transfer and access utilitiesfile transfer and access utilities
file transfer and access utilities
 
retrieving the mail
retrieving the mailretrieving the mail
retrieving the mail
 
connectivity utility
connectivity utilityconnectivity utility
connectivity utility
 
network hardware
network hardwarenetwork hardware
network hardware
 
ระบบเครือข่ายไร้สาย (wireless lan)
ระบบเครือข่ายไร้สาย (wireless lan)ระบบเครือข่ายไร้สาย (wireless lan)
ระบบเครือข่ายไร้สาย (wireless lan)
 
routing
routingrouting
routing
 
the transport layer
the transport layerthe transport layer
the transport layer
 
ระดับชั้นเน็ตเวิร์ก
ระดับชั้นเน็ตเวิร์กระดับชั้นเน็ตเวิร์ก
ระดับชั้นเน็ตเวิร์ก
 
ระดับชั้นดาต้าลิงค์
ระดับชั้นดาต้าลิงค์ระดับชั้นดาต้าลิงค์
ระดับชั้นดาต้าลิงค์
 
สถาปัตยกรรมเครือข่ายคอมพิวเตอร์และบริการ
สถาปัตยกรรมเครือข่ายคอมพิวเตอร์และบริการสถาปัตยกรรมเครือข่ายคอมพิวเตอร์และบริการ
สถาปัตยกรรมเครือข่ายคอมพิวเตอร์และบริการ
 
การส่งข้อมูลผ่านสายส่งและเทคนิคการส่งข้อมูลผ่านเครือข่าย
การส่งข้อมูลผ่านสายส่งและเทคนิคการส่งข้อมูลผ่านเครือข่ายการส่งข้อมูลผ่านสายส่งและเทคนิคการส่งข้อมูลผ่านเครือข่าย
การส่งข้อมูลผ่านสายส่งและเทคนิคการส่งข้อมูลผ่านเครือข่าย
 
ความรู้พื้นฐานของระบบการสื่อสารข้อมูล
ความรู้พื้นฐานของระบบการสื่อสารข้อมูลความรู้พื้นฐานของระบบการสื่อสารข้อมูล
ความรู้พื้นฐานของระบบการสื่อสารข้อมูล
 
พัฒนาเศรษฐกิจ
พัฒนาเศรษฐกิจพัฒนาเศรษฐกิจ
พัฒนาเศรษฐกิจ
 

Último

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 

Último (20)

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 

php basic sql

  • 1. PHP
  • 3. definition: Database • A database which structures data in the form of tables. Each table contains information relevant to a particular feature, and is linked to other tables by a common value. For example, two attribute tables could be linked to a spatial data table via a Geocode, such as the postcode.
  • 4. A very short explanation • In one database. There are many tables. • One table represents a set of entity. For example ‘People’ , ‘Cars’ , ‘Movies’ • Table has many rows. One row in table represents instance of entities. For example Toni Meechai’ , ‘Kobota Sedan’ , ‘Twilight’ • Table also has many column. One column represents property of an instance. For example Nationality’ , ‘How fast’ , ‘How boring it is’ • When you want get or do something with Database Tables, you can query it like ‘Please show me all people’. Unfortunately, you have to follow SQL format.
  • 5. Play with PHP MyAdmin • Pay attention to demonstration. • Try to understand ideas of ‘Database’ , ‘Table’, ‘Row’, ‘Basic usage of SQL’. • Learners can follow this article: http://www.php-editors.com/articles/sql_phpmyadmin.php
  • 7. Selecting from 1 table • SELECT * FROM COUNTRY; • SELECT * FROM COUNTRY WHERE CONTINECT=‘ASIA’; • SELECT CODE,NAME,CONTINENT,POPULATION FROM COUNTRY WHERE CONTINENT=‘ASIA’;
  • 8. • SELECT NAME,POPULATION FROM COUNTRY WHERE CONTINENT=‘ASIA’ ORDER BY POPULATION DESC; • If we want to reverse it, change DESC to ASC instead
  • 9. • If it has many rows, we want just 10 rows • SELECT NAME,POPULATION FROM COUNTRY WHERE CONTINENT=‘ASIA’ ORDER BY POPULATION DESC LIMIT 10;
  • 10. • If we want to change column name • SELECT NAME AS “ประเทศ สุ่ม” FROM COUNTRY WHERE CONTINENT=‘ASIA’ ORDER BY RAND() LIMIT 10; • NOTE, If you want to change name to be something like ประเทศ สุ่ม, it will throws an error • Because that word contains whitespace.
  • 11. • List all countries started with ‘t’ SELECT NAME FROM COUNTRY WHERE NAME LIKE ‘T%’; • List all countries ended with ‘land’ SELECT NAME FROM COUNTRY WHERE NAME LIKE ‘%land’; • List all countries contains ‘ko’ SELECT NAME FROM COUNTRY WHERE NAME LIKE ‘%ko%’;
  • 12. Tables joining Why joining? • Refer to database course. It is better to store different content into different table. Reducing data redundant. • MySQL, a database server we use is a Relational engine. We can say one table can has relationship to other tables. • To get data across tables, we have to do joining selection.
  • 13. Relationship in our 3 tables • Consider our 3 tables: Country, City, CountryLanguage • Table: Country has column ‘Code’ Table: City has column ‘CountryCode’ Table: CountryLanguage has column ‘CountryCode’ • Country.Code, City.CountryCode, CountryLanguage.CountryCode are sharing relationship.
  • 14. • List all city in Thailand SELECT * FROM CITY,COUNTRY WHERE CODE=‘THA’ AND CODE=COUNTRYCODE; • Remember. To join 2 tables we have to do 2 things. 1)Include 2 tables’name in From clause 2)Match the related columns in each 2 tables by using equality in where clause
  • 15. • If we have same columns name in 2 tables, you will get error cause there are ambiguous column names. • To fix this problem, specific table name before column name by using .(dot) SELECT CITY.NAME,COUNTRY.NAME FROM CITY,COUNTRY WHERE CODE=‘THA’ AND COUNTRY.CODE=CITY.COUNTRYCODE;
  • 16. Count and Sum • Find surface area of Thailand + Malaysia • SELECT SUM(SURFACEAREA) FROM COUNTRY WHERE NAME=‘THAILAND’ OR NAME=‘MALAYSIA’;
  • 17. • Find how many cities located in Asia • SELECT COUNT(CITY.NAME) FROM CITY,COUNTRY WHERE CONTINENT=‘ASIA’ AND COUNTRY.CODE=CITY.COUNTRYCODE;
  • 18. Changing Data Using SQL INSERT / UPDATE / DELETE
  • 19. Prepare table first. CREATE TABLE Student ( id bigint(20) auto_increment PRIMARY KEY, email varchar(255), firstname varchar(255), lastname varchar(255), nick varchar(255) );
  • 20. Inserting • — 1. Use SQL: “DESCRIBE tablename” to see the target table’s structure. Remember column name, type and sequence • 2. Use SQL: “INSERT INTO VALUES(xxx,yyy,zzz)” Replace xxx,yyy,zzz with actual values you want to insert ordering as the table column’s sequence.
  • 22. Updating • — Updating SQL syntax is UPDATE tablename SET columnanme = newvalue WHERE condition
  • 24. Deleting • Deleting SQL syntax is DELETE FROM tablename WHERE condition.
  • 25. DELETE FROM student WHERE firstname=‘Taw’;
  • 26. PHP interface with MySQL <?php mysql_connect("localhost:portnum", "user","password") or die(mysql_error()); mysql_select_db("dbname") or die(mysql_error()); $result = mysql_query("SELECT column1,column2 FROM tablename") or die(mysql_error()); while($row = mysql_fetch_array($result)) { print $row['column1']; print $row['column2']; } ?>