SlideShare a Scribd company logo
1 of 5
PHP Mysql connectivity
Use the mysql_connect( ) functiontoestablishedconnectiontothe MySQLserver.To accessthe
database functionalitywe have tomake a connectiontodatabase usingPhp.mysql_connect() function
isusedto establishthe connectiontomysql server.
Four argumentsneedtobe passedtomysql_connect() function.
Hostname : if you are working on local system , you can use localhost or you can also
provide ip address or server name.
username : if there is a existing user , you can provide username. default username is
'root'.
password : by default password is blank or null.
dbname : it is a optional field . it is basically a name of the database that need to be
connected.
mysql_connect(host, username, password, dbname);
Parameter Description
host(Server name) Either a host name(server name) or an IP address
username The MySQL user name
password The password to log in with
dbname Optional. The database to be used when performing queries
Note : There are more available parameters, but the ones listed above are the most
important. In the following example we store the connection in a variable ($con) for later
use in the script
<?php
// Create connection
$con=mysql_connect("localhost","root","") or die(mysql_error());
?>
Here localhost is server name. root is MySQL default user name. default password is blank and
database name is my_db. mysql_error( ) function provides mysql connectivity error message.
MySQL Close Connection
<?php
// Create connection
$con=mysql_connect("localhost","root","","my_db") or die(mysql_error());
//code to be executed...
// Close connection
mysql_close($con);
?>
after work with the database is done we have to close the connection using mysql_close()
function in which the connection to the database is passed.
Mysqli PHP Connectivity
How to connect PHP through mysqli. Create a registration form with 4 four fields
name,email id, mobile number and address. Fill all the fields click on save data submit
button to save your data inside "users" table using mysqli prepared statements insert.
To display all the values from "users" table click on disp data button it will display the
values using mysqli prepared statements select.
Create Database and Table
Database name:demo
Table:user
PHP Script(PHP mysqli connection )
<?php
//mysqli connectivity
$connect= new mysqli("localhost","root","","demo") or die("ERROR:could not
connect to the database!!!!");
extract($_POST);
if(isset($save))
{
$query="insert into users values('','$n','$e','$m','$add')";
if($connect->query($query))
{
echo 'congrates!! data saved';
}
else
{
echo 'data not saved '.$connect->error;
}
}
//display data
if(isset($disp))
{
$query="select * from users";
$result=$connect->query($query);
echo "<table border=1>";
echo "<tr><th>Name</th><th>Email</th><th>Mobile<
/th><th>Address</th></tr>";
while($row=$result->fetch_array())
{
echo "<tr>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['mobile']."</td>";
echo "<td>".$row['address']."</td>";
echo "</tr>";
}
echo "</table>";
}
?>
HTML Form
<style>
input,textarea{width:200px}
input[type=submit]{width:100px}
</style>
<form method="post">
<table width="218" border="0">
<tr>
<td width="208"><input type="text" name="n" placeholder="Enter your
name"/></td>
</tr>
<tr>
<td><input type="text" name="e" placeholder="Enter your email"/></td>
</tr>
<tr>
<td><input type="text" name="m" placeholder="Enter your mobile"/></td>
</tr>
<tr>
<td><textarea name="add" placeholder="Enter your address"></textarea></td>
</tr>
<tr>
<td>
<input type="submit" name="save" value="Save Data"/>
<input type="submit" name="disp" value="Display Data"/>
</td>
</tr>
</table>
</form>
Php mysql connectivity

More Related Content

What's hot (19)

MySQLi
MySQLiMySQLi
MySQLi
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
Python my sql database connection
Python my sql   database connectionPython my sql   database connection
Python my sql database connection
 
Database presentation
Database presentationDatabase presentation
Database presentation
 
Python my SQL - create table
Python my SQL - create tablePython my SQL - create table
Python my SQL - create table
 
Lecture6 display data by okello erick
Lecture6 display data by okello erickLecture6 display data by okello erick
Lecture6 display data by okello erick
 
Synapse india reviews on php and sql
Synapse india reviews on php and sqlSynapse india reviews on php and sql
Synapse india reviews on php and sql
 
Cake PHP 3 Presentaion
Cake PHP 3 PresentaionCake PHP 3 Presentaion
Cake PHP 3 Presentaion
 
My_sql_with_php
My_sql_with_phpMy_sql_with_php
My_sql_with_php
 
Request dispacther interface ppt
Request dispacther interface pptRequest dispacther interface ppt
Request dispacther interface ppt
 
Semalt: 3 Steps To PHP Web Page Scraping Web
Semalt: 3 Steps To PHP Web Page Scraping WebSemalt: 3 Steps To PHP Web Page Scraping Web
Semalt: 3 Steps To PHP Web Page Scraping Web
 
Php
PhpPhp
Php
 
Using php with my sql
Using php with my sqlUsing php with my sql
Using php with my sql
 
Php verses my sql
Php verses my sqlPhp verses my sql
Php verses my sql
 
TYPO3 6.2 for extension developer
TYPO3 6.2 for extension developerTYPO3 6.2 for extension developer
TYPO3 6.2 for extension developer
 
Mysql connect
Mysql connectMysql connect
Mysql connect
 
Mysql & Php
Mysql & PhpMysql & Php
Mysql & Php
 
Php verses MySQL
Php verses MySQLPhp verses MySQL
Php verses MySQL
 

Similar to Php mysql connectivity

Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docxModule 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docxmoirarandell
 
Connecting to my sql using PHP
Connecting to my sql using PHPConnecting to my sql using PHP
Connecting to my sql using PHPNisa Soomro
 
Collection of built in functions for manipulating MySQL databases.docx
Collection of built in functions for manipulating MySQL databases.docxCollection of built in functions for manipulating MySQL databases.docx
Collection of built in functions for manipulating MySQL databases.docxKingKhaliilHayat
 
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do
Php &amp; my sql  - how do pdo, mysq-li, and x devapi do what they doPhp &amp; my sql  - how do pdo, mysq-li, and x devapi do what they do
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they doDave Stokes
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesRasan Samarasinghe
 
All Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for NewbiesAll Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for NewbiesDave Stokes
 
Chapter 3.1.pptx
Chapter 3.1.pptxChapter 3.1.pptx
Chapter 3.1.pptxmebratu9
 
Mysql connection
Mysql connectionMysql connection
Mysql connectionbeben benzy
 
Learn PHP Lacture2
Learn PHP Lacture2Learn PHP Lacture2
Learn PHP Lacture2ADARSH BHATT
 
Rits Brown Bag - PHP & PHPMyAdmin
Rits Brown Bag - PHP & PHPMyAdminRits Brown Bag - PHP & PHPMyAdmin
Rits Brown Bag - PHP & PHPMyAdminRight IT Services
 
Php MySql For Beginners
Php MySql For BeginnersPhp MySql For Beginners
Php MySql For BeginnersPriti Solanki
 
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
 
Php Applications with Oracle by Kuassi Mensah
Php Applications with Oracle by Kuassi MensahPhp Applications with Oracle by Kuassi Mensah
Php Applications with Oracle by Kuassi MensahPHP Barcelona Conference
 

Similar to Php mysql connectivity (20)

Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docxModule 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
 
Connecting to my sql using PHP
Connecting to my sql using PHPConnecting to my sql using PHP
Connecting to my sql using PHP
 
Collection of built in functions for manipulating MySQL databases.docx
Collection of built in functions for manipulating MySQL databases.docxCollection of built in functions for manipulating MySQL databases.docx
Collection of built in functions for manipulating MySQL databases.docx
 
PHP and Mysql
PHP and MysqlPHP and Mysql
PHP and Mysql
 
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do
Php &amp; my sql  - how do pdo, mysq-li, and x devapi do what they doPhp &amp; my sql  - how do pdo, mysq-li, and x devapi do what they do
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do
 
PHP with MySQL
PHP with MySQLPHP with MySQL
PHP with MySQL
 
Php 2
Php 2Php 2
Php 2
 
Node.js with MySQL.pdf
Node.js with MySQL.pdfNode.js with MySQL.pdf
Node.js with MySQL.pdf
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL Databases
 
All Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for NewbiesAll Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for Newbies
 
Chapter 3.1.pptx
Chapter 3.1.pptxChapter 3.1.pptx
Chapter 3.1.pptx
 
Mysql connection
Mysql connectionMysql connection
Mysql connection
 
Mysql DBI
Mysql DBIMysql DBI
Mysql DBI
 
Learn PHP Lacture2
Learn PHP Lacture2Learn PHP Lacture2
Learn PHP Lacture2
 
harry presentation
harry presentationharry presentation
harry presentation
 
Php mysq
Php mysqPhp mysq
Php mysq
 
Rits Brown Bag - PHP & PHPMyAdmin
Rits Brown Bag - PHP & PHPMyAdminRits Brown Bag - PHP & PHPMyAdmin
Rits Brown Bag - PHP & PHPMyAdmin
 
Php MySql For Beginners
Php MySql For BeginnersPhp MySql For Beginners
Php MySql For Beginners
 
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
 
Php Applications with Oracle by Kuassi Mensah
Php Applications with Oracle by Kuassi MensahPhp Applications with Oracle by Kuassi Mensah
Php Applications with Oracle by Kuassi Mensah
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 

Recently uploaded (20)

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 

Php mysql connectivity

  • 1. PHP Mysql connectivity Use the mysql_connect( ) functiontoestablishedconnectiontothe MySQLserver.To accessthe database functionalitywe have tomake a connectiontodatabase usingPhp.mysql_connect() function isusedto establishthe connectiontomysql server. Four argumentsneedtobe passedtomysql_connect() function. Hostname : if you are working on local system , you can use localhost or you can also provide ip address or server name. username : if there is a existing user , you can provide username. default username is 'root'. password : by default password is blank or null. dbname : it is a optional field . it is basically a name of the database that need to be connected. mysql_connect(host, username, password, dbname); Parameter Description host(Server name) Either a host name(server name) or an IP address username The MySQL user name password The password to log in with dbname Optional. The database to be used when performing queries Note : There are more available parameters, but the ones listed above are the most important. In the following example we store the connection in a variable ($con) for later use in the script <?php // Create connection $con=mysql_connect("localhost","root","") or die(mysql_error()); ?> Here localhost is server name. root is MySQL default user name. default password is blank and database name is my_db. mysql_error( ) function provides mysql connectivity error message. MySQL Close Connection <?php // Create connection $con=mysql_connect("localhost","root","","my_db") or die(mysql_error()); //code to be executed...
  • 2. // Close connection mysql_close($con); ?> after work with the database is done we have to close the connection using mysql_close() function in which the connection to the database is passed. Mysqli PHP Connectivity How to connect PHP through mysqli. Create a registration form with 4 four fields name,email id, mobile number and address. Fill all the fields click on save data submit button to save your data inside "users" table using mysqli prepared statements insert. To display all the values from "users" table click on disp data button it will display the values using mysqli prepared statements select. Create Database and Table Database name:demo Table:user PHP Script(PHP mysqli connection ) <?php //mysqli connectivity $connect= new mysqli("localhost","root","","demo") or die("ERROR:could not connect to the database!!!!"); extract($_POST); if(isset($save)) { $query="insert into users values('','$n','$e','$m','$add')"; if($connect->query($query)) { echo 'congrates!! data saved'; }
  • 3. else { echo 'data not saved '.$connect->error; } } //display data if(isset($disp)) { $query="select * from users"; $result=$connect->query($query); echo "<table border=1>"; echo "<tr><th>Name</th><th>Email</th><th>Mobile< /th><th>Address</th></tr>"; while($row=$result->fetch_array()) { echo "<tr>"; echo "<td>".$row['name']."</td>"; echo "<td>".$row['email']."</td>"; echo "<td>".$row['mobile']."</td>"; echo "<td>".$row['address']."</td>"; echo "</tr>"; } echo "</table>"; } ?> HTML Form <style>
  • 4. input,textarea{width:200px} input[type=submit]{width:100px} </style> <form method="post"> <table width="218" border="0"> <tr> <td width="208"><input type="text" name="n" placeholder="Enter your name"/></td> </tr> <tr> <td><input type="text" name="e" placeholder="Enter your email"/></td> </tr> <tr> <td><input type="text" name="m" placeholder="Enter your mobile"/></td> </tr> <tr> <td><textarea name="add" placeholder="Enter your address"></textarea></td> </tr> <tr> <td> <input type="submit" name="save" value="Save Data"/> <input type="submit" name="disp" value="Display Data"/> </td> </tr> </table> </form>