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
PDF
4.3 MySQL + PHP
PPT
MYSQL - PHP Database Connectivity
ODP
Database Connection With Mysql
PPT
Php with MYSQL Database
PDF
lab56_db
PPT
Database presentation
Database Connectivity in PHP
4.3 MySQL + PHP
MYSQL - PHP Database Connectivity
Database Connection With Mysql
Php with MYSQL Database
lab56_db
Database presentation

What's hot (19)

PPT
PHP - Getting good with MySQL part II
PPTX
Mysql
PDF
PHP and Mysql
PDF
PHP with MySQL
PPTX
Learn PHP Lacture2
PPT
Synapse india reviews on php and sql
PDF
MySQL for beginners
PDF
Mysql & Php
PPT
PPT
PHP - PDO Objects
PPT
Working with Databases and MySQL
PPTX
Cake PHP 3 Presentaion
PDF
Using php with my sql
PDF
PDO Basics - PHPMelb 2014
PPTX
MySql:Basics
PPTX
MySql:Introduction
PPTX
Introduction to database
PPT
9780538745840 ppt ch06
PPTX
Php and database functionality
PHP - Getting good with MySQL part II
Mysql
PHP and Mysql
PHP with MySQL
Learn PHP Lacture2
Synapse india reviews on php and sql
MySQL for beginners
Mysql & Php
PHP - PDO Objects
Working with Databases and MySQL
Cake PHP 3 Presentaion
Using php with my sql
PDO Basics - PHPMelb 2014
MySql:Basics
MySql:Introduction
Introduction to database
9780538745840 ppt ch06
Php and database functionality
Ad

Viewers also liked (6)

PPTX
TCP protocol flow control
PPT
Flow & Error Control
PPT
MySql slides (ppt)
PPT
PPT
Php Presentation
PPSX
Flow Control
TCP protocol flow control
Flow & Error Control
MySql slides (ppt)
Php Presentation
Flow Control
Ad

Similar to Introduction to php database connectivity (20)

PDF
Difference between mysql_fetch_array and mysql_fetch_assoc in PHP
PPTX
Drupal7 dbtng
PDF
Php summary
PPTX
php2.pptx
PPTX
This slide show will brief about database handling
PDF
The Zen of Lithium
PPTX
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
PPTX
UNIT V (5).pptx
DOCX
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
PDF
Dependency Injection
PPTX
MySql Interface database in sql python my.pptx
DOC
Creating a Simple PHP and MySQL-Based Login System
DOC
Ex[1].3 php db connectivity
PDF
Check username availability with vue.js and PHP
PDF
Power shell examples_v4
KEY
Unit testing zend framework apps
PPT
Mysocial databasequeries
PPT
Mysocial databasequeries
PPTX
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
PPTX
Drupal 8 database api
Difference between mysql_fetch_array and mysql_fetch_assoc in PHP
Drupal7 dbtng
Php summary
php2.pptx
This slide show will brief about database handling
The Zen of Lithium
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
UNIT V (5).pptx
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
Dependency Injection
MySql Interface database in sql python my.pptx
Creating a Simple PHP and MySQL-Based Login System
Ex[1].3 php db connectivity
Check username availability with vue.js and PHP
Power shell examples_v4
Unit testing zend framework apps
Mysocial databasequeries
Mysocial databasequeries
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
Drupal 8 database api

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

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

Recently uploaded (20)

PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
Chapter 2 Digital Image Fundamentals.pdf
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Big Data Technologies - Introduction.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
KodekX | Application Modernization Development
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Chapter 2 Digital Image Fundamentals.pdf
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Advanced Soft Computing BINUS July 2025.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
MYSQL Presentation for SQL database connectivity
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Diabetes mellitus diagnosis method based random forest with bat algorithm
Reach Out and Touch Someone: Haptics and Empathic Computing
20250228 LYD VKU AI Blended-Learning.pptx
Empathic Computing: Creating Shared Understanding
Advanced methodologies resolving dimensionality complications for autism neur...
Big Data Technologies - Introduction.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
KodekX | Application Modernization Development
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
NewMind AI Weekly Chronicles - August'25 Week I

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]