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

More Related Content

What's hot (20)

Javascript
JavascriptJavascript
Javascript
 
PHP Cookies and Sessions
PHP Cookies and SessionsPHP Cookies and Sessions
PHP Cookies and Sessions
 
php
phpphp
php
 
Html and css
Html and cssHtml and css
Html and css
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
Php string function
Php string function Php string function
Php string function
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Introduction to XHTML
Introduction to XHTMLIntroduction to XHTML
Introduction to XHTML
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Html ppt
Html pptHtml ppt
Html ppt
 
JavaScript Control Statements I
JavaScript Control Statements IJavaScript Control Statements I
JavaScript Control Statements I
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
html-table
html-tablehtml-table
html-table
 
Javascript dom event
Javascript dom eventJavascript dom event
Javascript dom event
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
 
XSLT.ppt
XSLT.pptXSLT.ppt
XSLT.ppt
 
Css
CssCss
Css
 
Introduction of Html/css/js
Introduction of Html/css/jsIntroduction of Html/css/js
Introduction of Html/css/js
 

Similar to Database Connectivity in PHP

PHP and MySQL.pptx
PHP and MySQL.pptxPHP and MySQL.pptx
PHP and MySQL.pptxnatesanp1234
 
Connecting to my sql using PHP
Connecting to my sql using PHPConnecting to my sql using PHP
Connecting to my sql using PHPNisa 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 IIFirdaus Adib
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesRasan 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 7Mohd Harris Ahmad Jaal
 
PythonDatabaseAPI -Presentation for Database
PythonDatabaseAPI -Presentation for DatabasePythonDatabaseAPI -Presentation for Database
PythonDatabaseAPI -Presentation for Databasedharawagh9999
 
Chapter 3.1.pptx
Chapter 3.1.pptxChapter 3.1.pptx
Chapter 3.1.pptxmebratu9
 
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, 2019Dave Stokes
 
Lecture 15 - MySQL- PHP 1.ppt
Lecture 15 - MySQL- PHP 1.pptLecture 15 - MySQL- PHP 1.ppt
Lecture 15 - MySQL- PHP 1.pptTempMail233488
 
JDBC Connectivity Model
JDBC Connectivity ModelJDBC Connectivity Model
JDBC Connectivity Modelkunj desai
 
Learn PHP Lacture2
Learn PHP Lacture2Learn PHP Lacture2
Learn PHP Lacture2ADARSH BHATT
 
NonStop SQL/MX DBS Explained
NonStop SQL/MX DBS ExplainedNonStop SQL/MX DBS Explained
NonStop SQL/MX DBS ExplainedFrans Jongma
 
PHP and database functionality
PHP and database functionalityPHP and database functionality
PHP and database functionalitySayed Ahmed
 

Similar to 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
 

More from Taha Malampatti

Cultural heritage tourism
Cultural heritage tourismCultural heritage tourism
Cultural heritage tourismTaha Malampatti
 
Request dispacther interface ppt
Request dispacther interface pptRequest dispacther interface ppt
Request dispacther interface pptTaha Malampatti
 
Introduction to Android ppt
Introduction to Android pptIntroduction to Android ppt
Introduction to Android pptTaha Malampatti
 
Cox and Kings Pvt Industrial Training
Cox and Kings Pvt Industrial TrainingCox and Kings Pvt Industrial Training
Cox and Kings Pvt Industrial TrainingTaha 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 LCDTaha 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 LEDTaha Malampatti
 
The sunsparc architecture
The sunsparc architectureThe sunsparc architecture
The sunsparc architectureTaha Malampatti
 
Compressors and its applications
Compressors and its applicationsCompressors and its applications
Compressors and its applicationsTaha Malampatti
 

More from Taha Malampatti (16)

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

Recently uploaded

Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdfKamal Acharya
 
Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageRCC Institute of Information Technology
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringC Sai Kiran
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionjeevanprasad8
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdfKamal Acharya
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdfKamal Acharya
 
IT-601 Lecture Notes-UNIT-2.pdf Data Analysis
IT-601 Lecture Notes-UNIT-2.pdf Data AnalysisIT-601 Lecture Notes-UNIT-2.pdf Data Analysis
IT-601 Lecture Notes-UNIT-2.pdf Data AnalysisDr. Radhey Shyam
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfPipe Restoration Solutions
 
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfRESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfKamal Acharya
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxR&R Consult
 
Explosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdfExplosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdf884710SadaqatAli
 
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationKIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationDr. Radhey Shyam
 
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...Amil baba
 
Furniture showroom management system project.pdf
Furniture showroom management system project.pdfFurniture showroom management system project.pdf
Furniture showroom management system project.pdfKamal Acharya
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationRobbie Edward Sayers
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.PrashantGoswami42
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsAtif Razi
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdfKamal Acharya
 
Pharmacy management system project report..pdf
Pharmacy management system project report..pdfPharmacy management system project report..pdf
Pharmacy management system project report..pdfKamal Acharya
 

Recently uploaded (20)

Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltage
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projection
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdf
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
 
IT-601 Lecture Notes-UNIT-2.pdf Data Analysis
IT-601 Lecture Notes-UNIT-2.pdf Data AnalysisIT-601 Lecture Notes-UNIT-2.pdf Data Analysis
IT-601 Lecture Notes-UNIT-2.pdf Data Analysis
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfRESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Explosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdfExplosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdf
 
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationKIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
 
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
 
Furniture showroom management system project.pdf
Furniture showroom management system project.pdfFurniture showroom management system project.pdf
Furniture showroom management system project.pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Pharmacy management system project report..pdf
Pharmacy management system project report..pdfPharmacy management system project report..pdf
Pharmacy management system project report..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