SlideShare a Scribd company logo
PHP – Mysql connectivity
How to connect PHP with MySQL
1. Connecting to MySQL Server
• mysql_connect(ā€˜host_name’,’username’,’password’);
2. Selecting Database
• mysql_select_db(ā€˜Database Name’);
3. Query execution
• mysql_query(ā€šquery to be executed‛);
Sample Application
Example Program
<html>
<body>
<form action=‛registration.php‛ method=POST>
<input type=ā€štext‛ name=ā€šname‛>
<input type=ā€štext‛ name=ā€šemail‛>
</form>
</body>
</html>
Registration.php will look like this
<?PHP
$name = $_POST['name'];
$email = $_POST['email'];
mysql_connect("localhost","root","");
mysql_select_db("test2");
mysql_query ("insert into tbl_user(vchr_name,vchr_email) values ('$name','$email')");
echo "succesfully insertedā€š;
?>
Retrieving data from mysql
• There are 4 ways we can retrieve data from mysql tables
ļ‚§Mysql_fetch_array()
ļ‚§Mysql_fetch_row()
ļ‚§Mysql_fetch_assoc()
ļ‚§Mysql_fetch_object()
• All of the above will return one row from table at a time and then
the next row and so on . So usually we use these functions along
with a while loop
Retrieving data from mysql – mysql_fetch_assoc()
$query = mysql_query (ā€šselect * from tbl_customer");
while($fetch=mysql_fetch_assoc($query))
{
echo $fetch*ā€˜name’+;
echo $fetch*ā€˜vchr_email’+;
}
Retrieving data from mysql – mysql_fetch_row()
$query = mysql_query (ā€šselect * from tbl_customer");
while($fetch=mysql_fetch_array($query))
{
echo $fetch[0]; //prints first column the retrieved row
echo $fetch[1]; //prints second column the retrieved row
}
Retrieving data from mysql – mysql_fetch_array()
$query = mysql_query (ā€šselect * from tbl_customer");
while($fetch=mysql_fetch_array($query))
{
echo $fetch[0]; //prints first column the retrieved row. Ie name
echo $fetch[1]; //prints second column the retrieved row. Ie email
echo $fetch*ā€˜name’+; //prints first column the retrieved row
echo $fetch*ā€˜email’+; //prints second column the retrieved row
}
ā€šMysql_fetch_array() is a combination of mysql_fetch_assoc() and mysql_fetch_row()‛
Retrieving data from mysql – mysql_fetch_object()
$query = mysql_query (ā€šselect * from tbl_customer");
while($fetch=mysql_fetch_array($query))
{
echo $fetch->name; //prints first column the retrieved row
echo $fetch->email; //prints second column the retrieved row
}
ā€šReturns an object with properties that correspond to the fetched row and moves the
internal data pointer ahead.‛
Mysql_num_rows()
• PHP provides another utility inbuilt function mysql_num_rows()
which will return the number of rows returned from the executed
query
– Example
$query = mysql_query (ā€šselect * from tbl_customer");
$num=mysql_num_rows($query);
if($num>0)
{
while($fetch=mysql_fetch_array($query)
{ .................. }
Questions?
ā€šA good question deserve a good grade…‛
End of day
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com

More Related Content

PPTX
Database Connectivity in PHP
Taha Malampatti
Ā 
PDF
4.3 MySQL + PHP
Jalpesh Vasa
Ā 
PPTX
Php database connectivity
baabtra.com - No. 1 supplier of quality freshers
Ā 
PPT
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
Ā 
ODP
Database Connection With Mysql
Harit Kothari
Ā 
PPT
Php with MYSQL Database
Computer Hardware & Trouble shooting
Ā 
PDF
lab56_db
tutorialsruby
Ā 
PPT
Database presentation
webhostingguy
Ā 
Database Connectivity in PHP
Taha Malampatti
Ā 
4.3 MySQL + PHP
Jalpesh Vasa
Ā 
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
Ā 
Database Connection With Mysql
Harit Kothari
Ā 
Php with MYSQL Database
Computer Hardware & Trouble shooting
Ā 
lab56_db
tutorialsruby
Ā 
Database presentation
webhostingguy
Ā 

What's hot (19)

PPT
PHP - Getting good with MySQL part II
Firdaus Adib
Ā 
PPTX
Mysql
lotlot
Ā 
PDF
PHP and Mysql
Sankhadeep Roy
Ā 
PDF
PHP with MySQL
wahidullah mudaser
Ā 
PPTX
Learn PHP Lacture2
ADARSH BHATT
Ā 
PPT
Synapse india reviews on php and sql
saritasingh19866
Ā 
PDF
MySQL for beginners
Saeid Zebardast
Ā 
PDF
Mysql & Php
Inbal Geffen
Ā 
PPT
MySQL
Gouthaman V
Ā 
PPT
PHP - PDO Objects
AJINKYA N
Ā 
PPT
Working with Databases and MySQL
Nicole Ryan
Ā 
PPTX
Cake PHP 3 Presentaion
glslarmenta
Ā 
PDF
Using php with my sql
salissal
Ā 
PDF
PDO Basics - PHPMelb 2014
andrewdotcom
Ā 
PPTX
MySql:Basics
DataminingTools Inc
Ā 
PPTX
MySql:Introduction
DataminingTools Inc
Ā 
PPTX
Introduction to database
Pongsakorn U-chupala
Ā 
PPT
9780538745840 ppt ch06
Terry Yoast
Ā 
PPTX
Php and database functionality
Sayed Ahmed
Ā 
PHP - Getting good with MySQL part II
Firdaus Adib
Ā 
Mysql
lotlot
Ā 
PHP and Mysql
Sankhadeep Roy
Ā 
PHP with MySQL
wahidullah mudaser
Ā 
Learn PHP Lacture2
ADARSH BHATT
Ā 
Synapse india reviews on php and sql
saritasingh19866
Ā 
MySQL for beginners
Saeid Zebardast
Ā 
Mysql & Php
Inbal Geffen
Ā 
MySQL
Gouthaman V
Ā 
PHP - PDO Objects
AJINKYA N
Ā 
Working with Databases and MySQL
Nicole Ryan
Ā 
Cake PHP 3 Presentaion
glslarmenta
Ā 
Using php with my sql
salissal
Ā 
PDO Basics - PHPMelb 2014
andrewdotcom
Ā 
MySql:Basics
DataminingTools Inc
Ā 
MySql:Introduction
DataminingTools Inc
Ā 
Introduction to database
Pongsakorn U-chupala
Ā 
9780538745840 ppt ch06
Terry Yoast
Ā 
Php and database functionality
Sayed Ahmed
Ā 
Ad

Viewers also liked (6)

PPTX
TCP protocol flow control
anuragjagetiya
Ā 
PPT
Flow & Error Control
tameemyousaf
Ā 
PPT
MySql slides (ppt)
webhostingguy
Ā 
PPT
Php mysql ppt
Karmatechnologies Pvt. Ltd.
Ā 
PPT
Php Presentation
Manish Bothra
Ā 
PPSX
Flow Control
selvakumar_b1985
Ā 
TCP protocol flow control
anuragjagetiya
Ā 
Flow & Error Control
tameemyousaf
Ā 
MySql slides (ppt)
webhostingguy
Ā 
Php Presentation
Manish Bothra
Ā 
Flow Control
selvakumar_b1985
Ā 
Ad

Similar to Introduction to php database connectivity (20)

PDF
Difference between mysql_fetch_array and mysql_fetch_assoc in PHP
Vineet Kumar Saini
Ā 
PPTX
Drupal7 dbtng
Nicolas Leroy
Ā 
PDF
Php summary
Michelle Darling
Ā 
PPTX
php2.pptx
ElieNGOMSEU
Ā 
PPTX
This slide show will brief about database handling
averynight005
Ā 
PDF
The Zen of Lithium
Nate Abele
Ā 
PPTX
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
Ā 
PPTX
UNIT V (5).pptx
DrDhivyaaCRAssistant
Ā 
DOCX
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
moirarandell
Ā 
PDF
Dependency Injection
Rifat Nabi
Ā 
PPTX
MySql Interface database in sql python my.pptx
UshimArora
Ā 
DOC
Creating a Simple PHP and MySQL-Based Login System
Azharul Haque Shohan
Ā 
DOC
Ex[1].3 php db connectivity
Mouli Chandira
Ā 
PDF
Check username availability with vue.js and PHP
Yogesh singh
Ā 
PDF
Power shell examples_v4
JoeDinaso
Ā 
KEY
Unit testing zend framework apps
Michelangelo van Dam
Ā 
PPT
Mysocial databasequeries
Program in Interdisciplinary Computing
Ā 
PPT
Mysocial databasequeries
Program in Interdisciplinary Computing
Ā 
PPTX
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
berihun18
Ā 
PPTX
Drupal 8 database api
Viswanath Polaki
Ā 
Difference between mysql_fetch_array and mysql_fetch_assoc in PHP
Vineet Kumar Saini
Ā 
Drupal7 dbtng
Nicolas Leroy
Ā 
Php summary
Michelle Darling
Ā 
php2.pptx
ElieNGOMSEU
Ā 
This slide show will brief about database handling
averynight005
Ā 
The Zen of Lithium
Nate Abele
Ā 
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
Ā 
UNIT V (5).pptx
DrDhivyaaCRAssistant
Ā 
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
moirarandell
Ā 
Dependency Injection
Rifat Nabi
Ā 
MySql Interface database in sql python my.pptx
UshimArora
Ā 
Creating a Simple PHP and MySQL-Based Login System
Azharul Haque Shohan
Ā 
Ex[1].3 php db connectivity
Mouli Chandira
Ā 
Check username availability with vue.js and PHP
Yogesh singh
Ā 
Power shell examples_v4
JoeDinaso
Ā 
Unit testing zend framework apps
Michelangelo van Dam
Ā 
Mysocial databasequeries
Program in Interdisciplinary Computing
Ā 
Mysocial databasequeries
Program in Interdisciplinary Computing
Ā 
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
berihun18
Ā 
Drupal 8 database api
Viswanath Polaki
Ā 

More from baabtra.com - No. 1 supplier of quality freshers (20)

PPTX
Agile methodology and scrum development
baabtra.com - No. 1 supplier of quality freshers
Ā 
PDF
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
Ā 
PDF
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
Ā 
PDF
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
Ā 
PPTX
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
Ā 
PPTX
Chapter 5 transactions and dcl statements
baabtra.com - No. 1 supplier of quality freshers
Ā 
PPTX
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
Ā 
PPTX
Chapter 3 stored procedures
baabtra.com - No. 1 supplier of quality freshers
Ā 
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
Ā 
PPTX
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
Ā 
PPTX
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
Ā 
Agile methodology and scrum development
baabtra.com - No. 1 supplier of quality freshers
Ā 
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
Ā 
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
Ā 
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
Ā 
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
Ā 
Chapter 5 transactions and dcl statements
baabtra.com - No. 1 supplier of quality freshers
Ā 
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
Ā 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
Ā 
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
Ā 
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
Ā 

Recently uploaded (20)

PDF
Software Development Company | KodekX
KodekX
Ā 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
Ā 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
Ā 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
Ā 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
Ā 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
Ā 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
Ā 
PPTX
IoT Sensor Integration 2025 Powering Smart Tech and Industrial Automation.pptx
Rejig Digital
Ā 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
Ā 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
Ā 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
Ā 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
Ā 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
Ā 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
Ā 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
Ā 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
Ā 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
Ā 
PPTX
Coupa-Overview _Assumptions presentation
annapureddyn
Ā 
PPT
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
Ā 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
Ā 
Software Development Company | KodekX
KodekX
Ā 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
Ā 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
Ā 
Brief History of Internet - Early Days of Internet
sutharharshit158
Ā 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
Ā 
Presentation about Hardware and Software in Computer
snehamodhawadiya
Ā 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
Ā 
IoT Sensor Integration 2025 Powering Smart Tech and Industrial Automation.pptx
Rejig Digital
Ā 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
Ā 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
Ā 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
Ā 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
Ā 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
Ā 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
Ā 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
Ā 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
Ā 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
Ā 
Coupa-Overview _Assumptions presentation
annapureddyn
Ā 
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
Ā 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
Ā 

Introduction to php database connectivity

  • 1. PHP – Mysql connectivity
  • 2. How to connect PHP with MySQL 1. Connecting to MySQL Server • mysql_connect(ā€˜host_name’,’username’,’password’); 2. Selecting Database • mysql_select_db(ā€˜Database Name’); 3. Query execution • mysql_query(ā€šquery to be executed‛);
  • 4. Example Program <html> <body> <form action=‛registration.php‛ method=POST> <input type=ā€štext‛ name=ā€šname‛> <input type=ā€štext‛ name=ā€šemail‛> </form> </body> </html>
  • 5. Registration.php will look like this <?PHP $name = $_POST['name']; $email = $_POST['email']; mysql_connect("localhost","root",""); mysql_select_db("test2"); mysql_query ("insert into tbl_user(vchr_name,vchr_email) values ('$name','$email')"); echo "succesfully insertedā€š; ?>
  • 6. Retrieving data from mysql • There are 4 ways we can retrieve data from mysql tables ļ‚§Mysql_fetch_array() ļ‚§Mysql_fetch_row() ļ‚§Mysql_fetch_assoc() ļ‚§Mysql_fetch_object() • All of the above will return one row from table at a time and then the next row and so on . So usually we use these functions along with a while loop
  • 7. Retrieving data from mysql – mysql_fetch_assoc() $query = mysql_query (ā€šselect * from tbl_customer"); while($fetch=mysql_fetch_assoc($query)) { echo $fetch*ā€˜name’+; echo $fetch*ā€˜vchr_email’+; }
  • 8. Retrieving data from mysql – mysql_fetch_row() $query = mysql_query (ā€šselect * from tbl_customer"); while($fetch=mysql_fetch_array($query)) { echo $fetch[0]; //prints first column the retrieved row echo $fetch[1]; //prints second column the retrieved row }
  • 9. Retrieving data from mysql – mysql_fetch_array() $query = mysql_query (ā€šselect * from tbl_customer"); while($fetch=mysql_fetch_array($query)) { echo $fetch[0]; //prints first column the retrieved row. Ie name echo $fetch[1]; //prints second column the retrieved row. Ie email echo $fetch*ā€˜name’+; //prints first column the retrieved row echo $fetch*ā€˜email’+; //prints second column the retrieved row } ā€šMysql_fetch_array() is a combination of mysql_fetch_assoc() and mysql_fetch_row()‛
  • 10. Retrieving data from mysql – mysql_fetch_object() $query = mysql_query (ā€šselect * from tbl_customer"); while($fetch=mysql_fetch_array($query)) { echo $fetch->name; //prints first column the retrieved row echo $fetch->email; //prints second column the retrieved row } ā€šReturns an object with properties that correspond to the fetched row and moves the internal data pointer ahead.‛
  • 11. Mysql_num_rows() • PHP provides another utility inbuilt function mysql_num_rows() which will return the number of rows returned from the executed query – Example $query = mysql_query (ā€šselect * from tbl_customer"); $num=mysql_num_rows($query); if($num>0) { while($fetch=mysql_fetch_array($query) { .................. }
  • 12. Questions? ā€šA good question deserve a good grade…‛
  • 14. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 15. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: [email protected]