SlideShare una empresa de Scribd logo
1 de 12
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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
php
phpphp
php
 
Php string function
Php string function Php string function
Php string function
 
Remote Method Invocation (RMI)
Remote Method Invocation (RMI)Remote Method Invocation (RMI)
Remote Method Invocation (RMI)
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
JDBC ppt
JDBC pptJDBC ppt
JDBC ppt
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Event handling
Event handlingEvent handling
Event handling
 
Files in java
Files in javaFiles in java
Files in java
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
ADO .Net
ADO .Net ADO .Net
ADO .Net
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
 
Php forms
Php formsPhp forms
Php forms
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 

Similar a Database Connectivity in PHP

Learn PHP Lacture2
Learn PHP Lacture2Learn PHP Lacture2
Learn PHP Lacture2
ADARSH BHATT
 

Similar a Database Connectivity in PHP (20)

PHP with MySQL
PHP with MySQLPHP with MySQL
PHP with MySQL
 
MYSQL-Database
MYSQL-DatabaseMYSQL-Database
MYSQL-Database
 
PHP and MySQL.pptx
PHP and MySQL.pptxPHP and MySQL.pptx
PHP and MySQL.pptx
 
Mysql
MysqlMysql
Mysql
 
Php summary
Php summaryPhp summary
Php summary
 
Connecting to my sql using PHP
Connecting to my sql using PHPConnecting to my sql using PHP
Connecting to my sql using PHP
 
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
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL Databases
 
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
 
MySQL Record Operations
MySQL Record OperationsMySQL Record Operations
MySQL Record Operations
 
PythonDatabaseAPI -Presentation for Database
PythonDatabaseAPI -Presentation for DatabasePythonDatabaseAPI -Presentation for Database
PythonDatabaseAPI -Presentation for Database
 
My sql1
My sql1My sql1
My sql1
 
Chapter 3.1.pptx
Chapter 3.1.pptxChapter 3.1.pptx
Chapter 3.1.pptx
 
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
 
qwe.ppt
qwe.pptqwe.ppt
qwe.ppt
 
Lecture 15 - MySQL- PHP 1.ppt
Lecture 15 - MySQL- PHP 1.pptLecture 15 - MySQL- PHP 1.ppt
Lecture 15 - MySQL- PHP 1.ppt
 
JDBC Connectivity Model
JDBC Connectivity ModelJDBC Connectivity Model
JDBC Connectivity Model
 
Learn PHP Lacture2
Learn PHP Lacture2Learn PHP Lacture2
Learn PHP Lacture2
 
NonStop SQL/MX DBS Explained
NonStop SQL/MX DBS ExplainedNonStop SQL/MX DBS Explained
NonStop SQL/MX DBS Explained
 
Php and database functionality
Php and database functionalityPhp and database functionality
Php and database functionality
 

Más de Taha Malampatti

Más de Taha Malampatti (17)

Lex & yacc
Lex & yaccLex & yacc
Lex & yacc
 
Cultural heritage tourism
Cultural heritage tourismCultural heritage tourism
Cultural heritage tourism
 
Request dispacther interface ppt
Request dispacther interface pptRequest dispacther interface ppt
Request dispacther interface ppt
 
Introduction to Android ppt
Introduction to Android pptIntroduction to Android ppt
Introduction to Android ppt
 
Intodcution to Html
Intodcution to HtmlIntodcution to Html
Intodcution to Html
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Cox and Kings Pvt Industrial Training
Cox and Kings Pvt Industrial TrainingCox and Kings Pvt Industrial Training
Cox and Kings Pvt Industrial Training
 
Steganography ppt
Steganography pptSteganography ppt
Steganography ppt
 
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
 
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
 
Java Virtual Machine
Java Virtual MachineJava Virtual Machine
Java Virtual Machine
 
The sunsparc architecture
The sunsparc architectureThe sunsparc architecture
The sunsparc architecture
 
Orthogonal Projection
Orthogonal ProjectionOrthogonal Projection
Orthogonal Projection
 
Apple inc
Apple incApple inc
Apple inc
 
Blood donation
Blood donationBlood donation
Blood donation
 
Compressors and its applications
Compressors and its applicationsCompressors and its applications
Compressors and its applications
 
Laws Of Gravitation
Laws Of GravitationLaws Of Gravitation
Laws Of Gravitation
 

Último

Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 

Último (20)

VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 

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