SlideShare una empresa de 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'];
}
?>

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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
 

Destacado (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
 

Similar 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
 

Similar 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
 

Más 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
 

Más 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

Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Último (20)

Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 

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']; } ?>