SlideShare a Scribd company logo
WEB TECHNOLOGY
DATABASE CONNECTIVITY IN PHP
PREPARED BY:
Taha Malampattiwala - 140050107052
PHP: Built-in Database Access
• PHP provides built-in database connectivity for a wide range of databases – MySQL,
PostgreSQL, Oracle, Berkeley DB, Informix, mSQL, Lotus Notes, and more – Starting
support for a specific database may involve PHP configuration steps.
• Another advantage of using a programming language that has been designed for the
creation of web apps.
MySQL and PHP
 Architecture diagram
Connecting to MySQL
 To connect to a database, need to create a connection – At lowest level, this is a network
connection – Involves a login sequence.(username/password)
 Since this is a relatively expensive step, web application environments: – Share
connections – Have multiple connections.
 Whether, and how many, are typical configuration items. In MySQL: –
Allow_persistent: whether to allow persistent connections – Max_persistent: the
maximum number of persistent connections – Max_links: max number of connections,
persistent and not – Connection_timeout: how long the persistent connection is left open.
 Can also use SSL to encrypt connection.
High-Level Process of Using MySQL
from PHP
• Create a database connection
• Select database you wish to use
• Perform a SQL query
• Do some processing on query results
• Close database connection
Creating Database Connection
• Use either mysql_connect or mysql_pconnect to create database connection –
mysql_connect: connection is closed at end of script (end of page) – mysql_pconnect:
creates persistent connection.
• connection remains even after end of the page.
• Parameters – Server – hostname of server – Username – username on the database –
Password – password on the database – New Link (mysql_connect only) – reuse
database connection created by previous call to mysql_connect – Client Flags.
• MYSQL_CLIENT_SSL :: Use SSL
• MYSQL_CLIENT_COMPRESS :: Compress data sent to MySQL
Selecting a Database
• mysql_select_db() –Pass it the database name
• Related: –mysql_list_dbs()
• List databases available –Mysql_list_tables()
• List database tables available
Perform SQL Query
• Create query string – $query = ‘SQL formatted string’ – $query = ‘SELECT * FROM
table’
• Submit query to database for processing – $result = mysql_query($query); – For
UPDATE, DELETE, DROP, etc, returns TRUE or FALSE – For SELECT, SHOW,
DESCRIBE or EXPLAIN, $result is an identifier for the results, and does not contain the
results themselves
• $result is called a “resource” in this case
• A result of FALSE indicates an error.
• If there is an error – mysql_error() returns error string from last MySQL call
Process Results
• Many functions exist to work with database results
• mysql_num_rows() – Number of rows in the result set – Useful for iterating over
result set
• mysql_fetch_array() – Returns a result row as an array – Can be associative or
numeric or both (default) – $row = mysql_fetch_array($result); – $row[‘column
name’] :: value comes from database row with specified column name – $row[0] ::
value comes from first field in result set
Process Results Loop
• Easy loop for processing results:
$result = mysql_query($qstring); $num_rows = mysql_num_rows($result);
for ($i=0; $i<$num_rows; $i++)
{
$row = mysql_fetch_array($result);
// take action on database results here
}
Closing Database Connection
• mysql_close()
–Closes database connection
–Only works for connections opened with
mysql_connect()
–Connections opened with mysql_pconnect() ignore this call
–Often not necessary to call this, as connections created by mysql_connect are closed at
the end of the script anyway
THANK YOU
Ad

More Related Content

What's hot (20)

PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
Zeeshan Ahmed
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
Php array
Php arrayPhp array
Php array
Nikul Shah
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
webhostingguy
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
BG Java EE Course
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
WebStackAcademy
 
Form Handling using PHP
Form Handling using PHPForm Handling using PHP
Form Handling using PHP
Nisa Soomro
 
php
phpphp
php
ajeetjhajharia
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
Taha Malampatti
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Php string function
Php string function Php string function
Php string function
Ravi Bhadauria
 
Introduction to Html5
Introduction to Html5Introduction to Html5
Introduction to Html5
www.netgains.org
 
JavaScript: Events Handling
JavaScript: Events HandlingJavaScript: Events Handling
JavaScript: Events Handling
Yuriy Bezgachnyuk
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
Tiji Thomas
 
Javascript event handler
Javascript event handlerJavascript event handler
Javascript event handler
Jesus Obenita Jr.
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
Bootstrap
BootstrapBootstrap
Bootstrap
Jadson Santos
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
Poojith Chowdhary
 

Similar to Database Connectivity in PHP (20)

PHP with MySQL
PHP with MySQLPHP with MySQL
PHP with MySQL
wahidullah mudaser
 
MYSQL-Database
MYSQL-DatabaseMYSQL-Database
MYSQL-Database
V.V.Vanniaperumal College for Women
 
PHP and MySQL.pptx
PHP and MySQL.pptxPHP and MySQL.pptx
PHP and MySQL.pptx
natesanp1234
 
Mysql
MysqlMysql
Mysql
guest817344
 
Php summary
Php summaryPhp summary
Php summary
Michelle Darling
 
Connecting to my sql using PHP
Connecting to my sql using PHPConnecting to my sql using PHP
Connecting to my sql using PHP
Nisa Soomro
 
PHP - Getting good with MySQL part II
 PHP - Getting good with MySQL part II PHP - Getting good with MySQL part II
PHP - Getting good with MySQL part II
Firdaus Adib
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL Databases
Rasan Samarasinghe
 
Web Application Development using PHP Chapter 7
Web Application Development using PHP Chapter 7Web Application Development using PHP Chapter 7
Web Application Development using PHP Chapter 7
Mohd Harris Ahmad Jaal
 
MySQL Record Operations
MySQL Record OperationsMySQL Record Operations
MySQL Record Operations
Jamshid Hashimi
 
PythonDatabaseAPI -Presentation for Database
PythonDatabaseAPI -Presentation for DatabasePythonDatabaseAPI -Presentation for Database
PythonDatabaseAPI -Presentation for Database
dharawagh9999
 
My sql1
My sql1My sql1
My sql1
Akash Gupta
 
Chapter 3.1.pptx
Chapter 3.1.pptxChapter 3.1.pptx
Chapter 3.1.pptx
mebratu9
 
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
Dave Stokes
 
Lecture 15 - MySQL- PHP 1.ppt
Lecture 15 - MySQL- PHP 1.pptLecture 15 - MySQL- PHP 1.ppt
Lecture 15 - MySQL- PHP 1.ppt
TempMail233488
 
qwe.ppt
qwe.pptqwe.ppt
qwe.ppt
Heru762601
 
JDBC Connectivity Model
JDBC Connectivity ModelJDBC Connectivity Model
JDBC Connectivity Model
kunj desai
 
Learn PHP Lacture2
Learn PHP Lacture2Learn PHP Lacture2
Learn PHP Lacture2
ADARSH BHATT
 
NonStop SQL/MX DBS Explained
NonStop SQL/MX DBS ExplainedNonStop SQL/MX DBS Explained
NonStop SQL/MX DBS Explained
Frans Jongma
 
Php and database functionality
Php and database functionalityPhp and database functionality
Php and database functionality
Sayed Ahmed
 
PHP and MySQL.pptx
PHP and MySQL.pptxPHP and MySQL.pptx
PHP and MySQL.pptx
natesanp1234
 
Connecting to my sql using PHP
Connecting to my sql using PHPConnecting to my sql using PHP
Connecting to my sql using PHP
Nisa Soomro
 
PHP - Getting good with MySQL part II
 PHP - Getting good with MySQL part II PHP - Getting good with MySQL part II
PHP - Getting good with MySQL part II
Firdaus Adib
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL Databases
Rasan Samarasinghe
 
Web Application Development using PHP Chapter 7
Web Application Development using PHP Chapter 7Web Application Development using PHP Chapter 7
Web Application Development using PHP Chapter 7
Mohd Harris Ahmad Jaal
 
PythonDatabaseAPI -Presentation for Database
PythonDatabaseAPI -Presentation for DatabasePythonDatabaseAPI -Presentation for Database
PythonDatabaseAPI -Presentation for Database
dharawagh9999
 
Chapter 3.1.pptx
Chapter 3.1.pptxChapter 3.1.pptx
Chapter 3.1.pptx
mebratu9
 
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
Dave Stokes
 
Lecture 15 - MySQL- PHP 1.ppt
Lecture 15 - MySQL- PHP 1.pptLecture 15 - MySQL- PHP 1.ppt
Lecture 15 - MySQL- PHP 1.ppt
TempMail233488
 
JDBC Connectivity Model
JDBC Connectivity ModelJDBC Connectivity Model
JDBC Connectivity Model
kunj desai
 
Learn PHP Lacture2
Learn PHP Lacture2Learn PHP Lacture2
Learn PHP Lacture2
ADARSH BHATT
 
NonStop SQL/MX DBS Explained
NonStop SQL/MX DBS ExplainedNonStop SQL/MX DBS Explained
NonStop SQL/MX DBS Explained
Frans Jongma
 
Php and database functionality
Php and database functionalityPhp and database functionality
Php and database functionality
Sayed Ahmed
 
Ad

More from Taha Malampatti (16)

Lex & yacc
Lex & yaccLex & yacc
Lex & yacc
Taha Malampatti
 
Cultural heritage tourism
Cultural heritage tourismCultural heritage tourism
Cultural heritage tourism
Taha Malampatti
 
Request dispacther interface ppt
Request dispacther interface pptRequest dispacther interface ppt
Request dispacther interface ppt
Taha Malampatti
 
Introduction to Android ppt
Introduction to Android pptIntroduction to Android ppt
Introduction to Android ppt
Taha Malampatti
 
Intodcution to Html
Intodcution to HtmlIntodcution to Html
Intodcution to Html
Taha Malampatti
 
Cox and Kings Pvt Industrial Training
Cox and Kings Pvt Industrial TrainingCox and Kings Pvt Industrial Training
Cox and Kings Pvt Industrial Training
Taha Malampatti
 
Steganography ppt
Steganography pptSteganography ppt
Steganography ppt
Taha Malampatti
 
An application of 8085 register interfacing with LCD
An application  of 8085 register interfacing with LCDAn application  of 8085 register interfacing with LCD
An application of 8085 register interfacing with LCD
Taha Malampatti
 
An application of 8085 register interfacing with LED
An application  of 8085 register interfacing with LEDAn application  of 8085 register interfacing with LED
An application of 8085 register interfacing with LED
Taha Malampatti
 
Java Virtual Machine
Java Virtual MachineJava Virtual Machine
Java Virtual Machine
Taha Malampatti
 
The sunsparc architecture
The sunsparc architectureThe sunsparc architecture
The sunsparc architecture
Taha Malampatti
 
Orthogonal Projection
Orthogonal ProjectionOrthogonal Projection
Orthogonal Projection
Taha Malampatti
 
Apple inc
Apple incApple inc
Apple inc
Taha Malampatti
 
Blood donation
Blood donationBlood donation
Blood donation
Taha Malampatti
 
Compressors and its applications
Compressors and its applicationsCompressors and its applications
Compressors and its applications
Taha Malampatti
 
Laws Of Gravitation
Laws Of GravitationLaws Of Gravitation
Laws Of Gravitation
Taha Malampatti
 
Cultural heritage tourism
Cultural heritage tourismCultural heritage tourism
Cultural heritage tourism
Taha Malampatti
 
Request dispacther interface ppt
Request dispacther interface pptRequest dispacther interface ppt
Request dispacther interface ppt
Taha Malampatti
 
Introduction to Android ppt
Introduction to Android pptIntroduction to Android ppt
Introduction to Android ppt
Taha Malampatti
 
Cox and Kings Pvt Industrial Training
Cox and Kings Pvt Industrial TrainingCox and Kings Pvt Industrial Training
Cox and Kings Pvt Industrial Training
Taha Malampatti
 
An application of 8085 register interfacing with LCD
An application  of 8085 register interfacing with LCDAn application  of 8085 register interfacing with LCD
An application of 8085 register interfacing with LCD
Taha Malampatti
 
An application of 8085 register interfacing with LED
An application  of 8085 register interfacing with LEDAn application  of 8085 register interfacing with LED
An application of 8085 register interfacing with LED
Taha Malampatti
 
The sunsparc architecture
The sunsparc architectureThe sunsparc architecture
The sunsparc architecture
Taha Malampatti
 
Compressors and its applications
Compressors and its applicationsCompressors and its applications
Compressors and its applications
Taha Malampatti
 
Ad

Recently uploaded (20)

Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
Building-Services-Introduction-Notes.pdf
Building-Services-Introduction-Notes.pdfBuilding-Services-Introduction-Notes.pdf
Building-Services-Introduction-Notes.pdf
Lawrence Omai
 
Novel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth ControlNovel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth Control
Chris Harding
 
How to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdfHow to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdf
jamedlimmk
 
Analog electronic circuits with some imp
Analog electronic circuits with some impAnalog electronic circuits with some imp
Analog electronic circuits with some imp
KarthikTG7
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .
NABLAS株式会社
 
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdfPRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Guru
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
Taqyea
 
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
roshinijoga
 
A Survey of Personalized Large Language Models.pptx
A Survey of Personalized Large Language Models.pptxA Survey of Personalized Large Language Models.pptx
A Survey of Personalized Large Language Models.pptx
rutujabhaskarraopati
 
Understanding Structural Loads and Load Paths
Understanding Structural Loads and Load PathsUnderstanding Structural Loads and Load Paths
Understanding Structural Loads and Load Paths
University of Kirkuk
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
Dynamics of Structures with Uncertain Properties.pptx
Dynamics of Structures with Uncertain Properties.pptxDynamics of Structures with Uncertain Properties.pptx
Dynamics of Structures with Uncertain Properties.pptx
University of Glasgow
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
Building-Services-Introduction-Notes.pdf
Building-Services-Introduction-Notes.pdfBuilding-Services-Introduction-Notes.pdf
Building-Services-Introduction-Notes.pdf
Lawrence Omai
 
Novel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth ControlNovel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth Control
Chris Harding
 
How to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdfHow to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdf
jamedlimmk
 
Analog electronic circuits with some imp
Analog electronic circuits with some impAnalog electronic circuits with some imp
Analog electronic circuits with some imp
KarthikTG7
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .
NABLAS株式会社
 
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdfPRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Guru
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
Taqyea
 
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
roshinijoga
 
A Survey of Personalized Large Language Models.pptx
A Survey of Personalized Large Language Models.pptxA Survey of Personalized Large Language Models.pptx
A Survey of Personalized Large Language Models.pptx
rutujabhaskarraopati
 
Understanding Structural Loads and Load Paths
Understanding Structural Loads and Load PathsUnderstanding Structural Loads and Load Paths
Understanding Structural Loads and Load Paths
University of Kirkuk
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
Dynamics of Structures with Uncertain Properties.pptx
Dynamics of Structures with Uncertain Properties.pptxDynamics of Structures with Uncertain Properties.pptx
Dynamics of Structures with Uncertain Properties.pptx
University of Glasgow
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 

Database Connectivity in PHP

  • 1. WEB TECHNOLOGY DATABASE CONNECTIVITY IN PHP PREPARED BY: Taha Malampattiwala - 140050107052
  • 2. PHP: Built-in Database Access • PHP provides built-in database connectivity for a wide range of databases – MySQL, PostgreSQL, Oracle, Berkeley DB, Informix, mSQL, Lotus Notes, and more – Starting support for a specific database may involve PHP configuration steps. • Another advantage of using a programming language that has been designed for the creation of web apps.
  • 3. MySQL and PHP  Architecture diagram
  • 4. Connecting to MySQL  To connect to a database, need to create a connection – At lowest level, this is a network connection – Involves a login sequence.(username/password)  Since this is a relatively expensive step, web application environments: – Share connections – Have multiple connections.  Whether, and how many, are typical configuration items. In MySQL: – Allow_persistent: whether to allow persistent connections – Max_persistent: the maximum number of persistent connections – Max_links: max number of connections, persistent and not – Connection_timeout: how long the persistent connection is left open.  Can also use SSL to encrypt connection.
  • 5. High-Level Process of Using MySQL from PHP • Create a database connection • Select database you wish to use • Perform a SQL query • Do some processing on query results • Close database connection
  • 6. Creating Database Connection • Use either mysql_connect or mysql_pconnect to create database connection – mysql_connect: connection is closed at end of script (end of page) – mysql_pconnect: creates persistent connection. • connection remains even after end of the page. • Parameters – Server – hostname of server – Username – username on the database – Password – password on the database – New Link (mysql_connect only) – reuse database connection created by previous call to mysql_connect – Client Flags. • MYSQL_CLIENT_SSL :: Use SSL • MYSQL_CLIENT_COMPRESS :: Compress data sent to MySQL
  • 7. Selecting a Database • mysql_select_db() –Pass it the database name • Related: –mysql_list_dbs() • List databases available –Mysql_list_tables() • List database tables available
  • 8. Perform SQL Query • Create query string – $query = ‘SQL formatted string’ – $query = ‘SELECT * FROM table’ • Submit query to database for processing – $result = mysql_query($query); – For UPDATE, DELETE, DROP, etc, returns TRUE or FALSE – For SELECT, SHOW, DESCRIBE or EXPLAIN, $result is an identifier for the results, and does not contain the results themselves • $result is called a “resource” in this case • A result of FALSE indicates an error. • If there is an error – mysql_error() returns error string from last MySQL call
  • 9. Process Results • Many functions exist to work with database results • mysql_num_rows() – Number of rows in the result set – Useful for iterating over result set • mysql_fetch_array() – Returns a result row as an array – Can be associative or numeric or both (default) – $row = mysql_fetch_array($result); – $row[‘column name’] :: value comes from database row with specified column name – $row[0] :: value comes from first field in result set
  • 10. Process Results Loop • Easy loop for processing results: $result = mysql_query($qstring); $num_rows = mysql_num_rows($result); for ($i=0; $i<$num_rows; $i++) { $row = mysql_fetch_array($result); // take action on database results here }
  • 11. Closing Database Connection • mysql_close() –Closes database connection –Only works for connections opened with mysql_connect() –Connections opened with mysql_pconnect() ignore this call –Often not necessary to call this, as connections created by mysql_connect are closed at the end of the script anyway
  翻译: