SlideShare a Scribd company logo
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'];
}
?>
Ad

Recommended

working with database using mysql
working with database using mysql
Subhasis Nayak
 
SQL for interview
SQL for interview
Aditya Kumar Tripathy
 
Introduction to SQL
Introduction to SQL
Ram Kedem
 
Introduction to database
Introduction to database
Pongsakorn U-chupala
 
Ms sql-server
Ms sql-server
Md.Mojibul Hoque
 
Database Architecture and Basic Concepts
Database Architecture and Basic Concepts
Tony Wong
 
Introduction to (sql)
Introduction to (sql)
Dattatray Ghorpade
 
intro for sql
intro for sql
mahmoud mones
 
SQL: Structured Query Language
SQL: Structured Query Language
Rohit Bisht
 
DDL DATA DEFINATION LANGUAGE
DDL DATA DEFINATION LANGUAGE
Abrar ali
 
Database Languges
Database Languges
Ahmed Rodeni
 
Sql a practical introduction
Sql a practical introduction
Hasan Kata
 
Mysql Crud, Php Mysql, php, sql
Mysql Crud, Php Mysql, php, sql
Aimal Miakhel
 
Structured query language(sql)ppt
Structured query language(sql)ppt
Gowarthini
 
Sql Basics And Advanced
Sql Basics And Advanced
rainynovember12
 
SQL Basics
SQL Basics
Hammad Rasheed
 
Html tables
Html tables
Sikandar Pandit
 
Introduction to SQL
Introduction to SQL
Ehsan Hamzei
 
SQL SERVER Training in Pune Slides
SQL SERVER Training in Pune Slides
enosislearningcom
 
INTRO TO SQL
INTRO TO SQL
Bro Shola Ajayi
 
Sql ppt
Sql ppt
Anuja Lad
 
History of computer
History of computer
San Mateo National High School/YneAhunin
 
Lesson 1: Scratch Computer Programming
Lesson 1: Scratch Computer Programming
SeniorInfants
 
Lang generations 7557_syed_ghazanfarnaqvi_saturday
Lang generations 7557_syed_ghazanfarnaqvi_saturday
Syed Naqvi
 
Les01 Writing Basic Sql Statements
Les01 Writing Basic Sql Statements
NETsolutions Asia: NSA – Thailand, Sripatum University: SPU
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
1keydata
 
Unit1 principle of programming language
Unit1 principle of programming language
Vasavi College of Engg
 
MYSQL database presentation slides with examples
MYSQL database presentation slides with examples
dhanishev1
 
SQL
SQL
Shahriar Robbani
 
Crash course in sql
Crash course in sql
Mithun Shanbhag
 

More Related Content

What's hot (12)

SQL: Structured Query Language
SQL: Structured Query Language
Rohit Bisht
 
DDL DATA DEFINATION LANGUAGE
DDL DATA DEFINATION LANGUAGE
Abrar ali
 
Database Languges
Database Languges
Ahmed Rodeni
 
Sql a practical introduction
Sql a practical introduction
Hasan Kata
 
Mysql Crud, Php Mysql, php, sql
Mysql Crud, Php Mysql, php, sql
Aimal Miakhel
 
Structured query language(sql)ppt
Structured query language(sql)ppt
Gowarthini
 
Sql Basics And Advanced
Sql Basics And Advanced
rainynovember12
 
SQL Basics
SQL Basics
Hammad Rasheed
 
Html tables
Html tables
Sikandar Pandit
 
Introduction to SQL
Introduction to SQL
Ehsan Hamzei
 
SQL SERVER Training in Pune Slides
SQL SERVER Training in Pune Slides
enosislearningcom
 
INTRO TO SQL
INTRO TO SQL
Bro Shola Ajayi
 
SQL: Structured Query Language
SQL: Structured Query Language
Rohit Bisht
 
DDL DATA DEFINATION LANGUAGE
DDL DATA DEFINATION LANGUAGE
Abrar ali
 
Sql a practical introduction
Sql a practical introduction
Hasan Kata
 
Mysql Crud, Php Mysql, php, sql
Mysql Crud, Php Mysql, php, sql
Aimal Miakhel
 
Structured query language(sql)ppt
Structured query language(sql)ppt
Gowarthini
 
Introduction to SQL
Introduction to SQL
Ehsan Hamzei
 
SQL SERVER Training in Pune Slides
SQL SERVER Training in Pune Slides
enosislearningcom
 

Viewers also liked (7)

Sql ppt
Sql ppt
Anuja Lad
 
History of computer
History of computer
San Mateo National High School/YneAhunin
 
Lesson 1: Scratch Computer Programming
Lesson 1: Scratch Computer Programming
SeniorInfants
 
Lang generations 7557_syed_ghazanfarnaqvi_saturday
Lang generations 7557_syed_ghazanfarnaqvi_saturday
Syed Naqvi
 
Les01 Writing Basic Sql Statements
Les01 Writing Basic Sql Statements
NETsolutions Asia: NSA – Thailand, Sripatum University: SPU
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
1keydata
 
Unit1 principle of programming language
Unit1 principle of programming language
Vasavi College of Engg
 
Ad

Similar to php basic sql (20)

MYSQL database presentation slides with examples
MYSQL database presentation slides with examples
dhanishev1
 
SQL
SQL
Shahriar Robbani
 
Crash course in sql
Crash course in sql
Mithun Shanbhag
 
Data Base Management 1 Database Management.pptx
Data Base Management 1 Database Management.pptx
PreeTVithule1
 
SQL Workshop Slide
SQL Workshop Slide
YukiKitayama
 
ADV Powepoint 2 Lec.pptx
ADV Powepoint 2 Lec.pptx
ArjayBalberan1
 
Introduction to SQL
Introduction to SQL
Amin Choroomi
 
Introduction to SQL
Introduction to SQL
lahin31
 
SQL Assessment Command Statements
SQL Assessment Command Statements
Shaun Wilson
 
INTRODUCTION TO SQL QUERIES REALTED BRIEF
INTRODUCTION TO SQL QUERIES REALTED BRIEF
VADAPALLYPRAVEENKUMA1
 
SQL Server Learning Drive
SQL Server Learning Drive
TechandMate
 
full detailled SQL notesquestion bank (1).pdf
full detailled SQL notesquestion bank (1).pdf
yvpachorib23
 
Explain2
Explain2
Anis Berejeb
 
SQl data base management and design
SQl data base management and design
franckelsania20
 
Sql practise for beginners
Sql practise for beginners
ISsoft
 
Avinash database
Avinash database
avibmas
 
Sql
Sql
pratik201289
 
CS 542 Overview of query processing
CS 542 Overview of query processing
J Singh
 
LECTURE NOTES.pdf
LECTURE NOTES.pdf
ChryslerPanaguiton
 
LECTURE NOTES.pdf
LECTURE NOTES.pdf
ChryslerPanaguiton
 
MYSQL database presentation slides with examples
MYSQL database presentation slides with examples
dhanishev1
 
Data Base Management 1 Database Management.pptx
Data Base Management 1 Database Management.pptx
PreeTVithule1
 
SQL Workshop Slide
SQL Workshop Slide
YukiKitayama
 
ADV Powepoint 2 Lec.pptx
ADV Powepoint 2 Lec.pptx
ArjayBalberan1
 
Introduction to SQL
Introduction to SQL
lahin31
 
SQL Assessment Command Statements
SQL Assessment Command Statements
Shaun Wilson
 
INTRODUCTION TO SQL QUERIES REALTED BRIEF
INTRODUCTION TO SQL QUERIES REALTED BRIEF
VADAPALLYPRAVEENKUMA1
 
SQL Server Learning Drive
SQL Server Learning Drive
TechandMate
 
full detailled SQL notesquestion bank (1).pdf
full detailled SQL notesquestion bank (1).pdf
yvpachorib23
 
SQl data base management and design
SQl data base management and design
franckelsania20
 
Sql practise for beginners
Sql practise for beginners
ISsoft
 
Avinash database
Avinash database
avibmas
 
CS 542 Overview of query processing
CS 542 Overview of query processing
J Singh
 
Ad

More from tumetr1 (20)

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

Recently uploaded (20)

How to Implement Least Package Removal Strategy in Odoo 18 Inventory
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
penafloridaarlyn
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
JHS SHS Back to School 2024-2025 .pptx
JHS SHS Back to School 2024-2025 .pptx
melvinapay78
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
penafloridaarlyn
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
JHS SHS Back to School 2024-2025 .pptx
JHS SHS Back to School 2024-2025 .pptx
melvinapay78
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 

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