SlideShare a Scribd company logo
http://www.tutorialspoint.com/mysql/mysql-connection.htm Copyright © tutorialspoint.com
MYSQL CONNECTIONMYSQL CONNECTION
MySQL Connection using mysql binary:
You can establish MySQL database using mysql binary at command prompt.
Example:
Here is a simple example to connect to MySQL server from command prompt:
[root@host]# mysql -u root -p
Enter password:******
This will give you mysql> command prompt where you will be able to execute any SQL command.
Following is the result of above command:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2854760 to server version: 5.0.9
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
In above example, we have used root as a user but you can use any other user. Any user will be
able to perform all the SQL operations, which are allowed to that user.
You can disconnect from MySQL database any time using exit command at mysql> prompt.
mysql> exit
Bye
MySQL Connection using PHP Script:
PHP provides mysql_connect() function to open a database connection. This function takes five
parameters and returns a MySQL link identifier on success or FALSE on failure.
Syntax:
connection mysql_connect(server,user,passwd,new_link,client_flag);
Parameter Description
server Optional - The host name running database server. If not specified, then
default value is localhost:3036.
user Optional - The username accessing the database. If not specified, then
default is the name of the user that owns the server process.
passwd Optional - The password of the user accessing the database. If not specified,
then default is an empty password.
new_link Optional - If a second call is made to mysql_connect() with the same
arguments, no new connection will be established; instead, the identifier of
the already opened connection will be returned.
client_flags Optional - A combination of the following constants:
MYSQL_CLIENT_SSL - Use SSL encryption
MYSQL_CLIENT_COMPRESS - Use compression protocol
MYSQL_CLIENT_IGNORE_SPACE - Allow space after function names
MYSQL_CLIENT_INTERACTIVE - Allow interactive timeout seconds of
inactivity before closing the connection
You can disconnect from MySQL database anytime using another PHP function mysql_close().
This function takes a single parameter, which is a connection returned by mysql_connect()
function.
Syntax:
bool mysql_close ( resource $link_identifier );
If a resource is not specified then last opened database is closed. This function returns true if it
closes connection successfully otherwise it returns false.
Example:
Try out the following example to connect to a MySQL server:
<html>
<head>
<title>Connecting MySQL Server</title>
</head>
<body>
<?php
$dbhost = 'localhost:3036';
$dbuser = 'guest';
$dbpass = 'guest123';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($conn);
?>
</body>
</html>

More Related Content

What's hot (13)

PPTX
Enabling Security For ActiveMQ JMX Access
Ramakrishna Narkedamilli
 
ODP
Sql installation
Balakumaran Arunachalam
 
PDF
Using XMPP JSONPatch for synchronizing an OpenUI5 Model
Manuel Blechschmidt
 
PPTX
Compress and decompress
Son Nguyen
 
PPTX
Caching and invalidating with managed store
irfan1008
 
PPTX
Caching and invalidating with managed store
F K
 
PPSX
Execute sql query or sql command sql server using command prompt
Ikhwan Krisnadi
 
PPTX
Splitting with mule part2
Anirban Sen Chowdhary
 
PPTX
Quartz in mule
Son Nguyen
 
PPTX
Mule idempotent filter and Object Store
Anirban Sen Chowdhary
 
PPTX
NodeJs Session02
Jainul Musani
 
DOCX
Query transport problems
Igor Prudnecionok
 
PPTX
Caching and invalidating with managed store
Anirban Sen Chowdhary
 
Enabling Security For ActiveMQ JMX Access
Ramakrishna Narkedamilli
 
Sql installation
Balakumaran Arunachalam
 
Using XMPP JSONPatch for synchronizing an OpenUI5 Model
Manuel Blechschmidt
 
Compress and decompress
Son Nguyen
 
Caching and invalidating with managed store
irfan1008
 
Caching and invalidating with managed store
F K
 
Execute sql query or sql command sql server using command prompt
Ikhwan Krisnadi
 
Splitting with mule part2
Anirban Sen Chowdhary
 
Quartz in mule
Son Nguyen
 
Mule idempotent filter and Object Store
Anirban Sen Chowdhary
 
NodeJs Session02
Jainul Musani
 
Query transport problems
Igor Prudnecionok
 
Caching and invalidating with managed store
Anirban Sen Chowdhary
 

Viewers also liked (8)

PPT
Five Common Challenges in Blogging and How to Overcome Them
Allison Jones
 
PDF
Tabison company profile 2017
Tabison Research
 
PPT
Should Journalists Blog?
John Wilpers
 
PDF
Mysql alter-command
beben benzy
 
PDF
Maggie Q
Jackqueline Lou
 
PPTX
role of agricultural policies in responding to food security in India
8902714972
 
PDF
Overture - Bjork
Partitura de Banda
 
PDF
Opendata and business - داده های باز و کسب و کار
efazati
 
Five Common Challenges in Blogging and How to Overcome Them
Allison Jones
 
Tabison company profile 2017
Tabison Research
 
Should Journalists Blog?
John Wilpers
 
Mysql alter-command
beben benzy
 
Maggie Q
Jackqueline Lou
 
role of agricultural policies in responding to food security in India
8902714972
 
Overture - Bjork
Partitura de Banda
 
Opendata and business - داده های باز و کسب و کار
efazati
 
Ad

Similar to Mysql connection (20)

PDF
Python my sql database connection
Learnbay Datascience
 
DOCX
Php mysql connectivity
abhikwb
 
PPTX
Php and database functionality
Sayed Ahmed
 
PPTX
PHP and database functionality
Sayed Ahmed
 
PPTX
Php and database functionality
Sayed Ahmed
 
DOCX
SSMS-waitstats
E Blake
 
PPTX
Active mq Installation and Master Slave setup
Ramakrishna Narkedamilli
 
DOCX
Multiple instances second method
Vasudeva Rao
 
PDF
Mysqlsecurityoptionsjan2021
sepehrdamavandi2
 
PDF
Node.js with MySQL.pdf
SudhanshiBakre1
 
PDF
PHP with MySQL
wahidullah mudaser
 
DOCX
Mater,slave on mysql
Vasudeva Rao
 
PPT
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
PDF
Using php with my sql
salissal
 
PPT
PHP - Getting good with MySQL part II
Firdaus Adib
 
PDF
MySQL Router REST API
Frederic Descamps
 
PPTX
Dprn3 u3 a1_ocov
Nicolay Stojakovic
 
PPTX
Connecting to my sql using PHP
Nisa Soomro
 
ODP
Mysql Proxy Presentation Yahoo
Chris Westin
 
ODP
Mysql proxy presentation_yahoo
Chris Westin
 
Python my sql database connection
Learnbay Datascience
 
Php mysql connectivity
abhikwb
 
Php and database functionality
Sayed Ahmed
 
PHP and database functionality
Sayed Ahmed
 
Php and database functionality
Sayed Ahmed
 
SSMS-waitstats
E Blake
 
Active mq Installation and Master Slave setup
Ramakrishna Narkedamilli
 
Multiple instances second method
Vasudeva Rao
 
Mysqlsecurityoptionsjan2021
sepehrdamavandi2
 
Node.js with MySQL.pdf
SudhanshiBakre1
 
PHP with MySQL
wahidullah mudaser
 
Mater,slave on mysql
Vasudeva Rao
 
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
Using php with my sql
salissal
 
PHP - Getting good with MySQL part II
Firdaus Adib
 
MySQL Router REST API
Frederic Descamps
 
Dprn3 u3 a1_ocov
Nicolay Stojakovic
 
Connecting to my sql using PHP
Nisa Soomro
 
Mysql Proxy Presentation Yahoo
Chris Westin
 
Mysql proxy presentation_yahoo
Chris Westin
 
Ad

Recently uploaded (20)

PPTX
一比一原版(SUNY-Albany毕业证)纽约州立大学奥尔巴尼分校毕业证如何办理
Taqyea
 
PDF
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
PPTX
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
PPT
introduction to networking with basics coverage
RamananMuthukrishnan
 
PPT
Agilent Optoelectronic Solutions for Mobile Application
andreashenniger2
 
PPTX
一比一原版(LaTech毕业证)路易斯安那理工大学毕业证如何办理
Taqyea
 
PPTX
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
PPTX
Presentation3gsgsgsgsdfgadgsfgfgsfgagsfgsfgzfdgsdgs.pptx
SUB03
 
PDF
Build Fast, Scale Faster: Milvus vs. Zilliz Cloud for Production-Ready AI
Zilliz
 
PPTX
sajflsajfljsdfljslfjslfsdfas;fdsfksadfjlsdflkjslgfs;lfjlsajfl;sajfasfd.pptx
theknightme
 
PPTX
西班牙武康大学毕业证书{UCAMOfferUCAM成绩单水印}原版制作
Taqyea
 
PPTX
Orchestrating things in Angular application
Peter Abraham
 
PDF
Apple_Environmental_Progress_Report_2025.pdf
yiukwong
 
PPTX
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
PPTX
PM200.pptxghjgfhjghjghjghjghjghjghjghjghjghj
breadpaan921
 
DOCX
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
PPTX
Optimization_Techniques_ML_Presentation.pptx
farispalayi
 
PDF
The-Hidden-Dangers-of-Skipping-Penetration-Testing.pdf.pdf
naksh4thra
 
PPT
Computer Securityyyyyyyy - Chapter 2.ppt
SolomonSB
 
PDF
𝐁𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓
hokimamad0
 
一比一原版(SUNY-Albany毕业证)纽约州立大学奥尔巴尼分校毕业证如何办理
Taqyea
 
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
introduction to networking with basics coverage
RamananMuthukrishnan
 
Agilent Optoelectronic Solutions for Mobile Application
andreashenniger2
 
一比一原版(LaTech毕业证)路易斯安那理工大学毕业证如何办理
Taqyea
 
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
Presentation3gsgsgsgsdfgadgsfgfgsfgagsfgsfgzfdgsdgs.pptx
SUB03
 
Build Fast, Scale Faster: Milvus vs. Zilliz Cloud for Production-Ready AI
Zilliz
 
sajflsajfljsdfljslfjslfsdfas;fdsfksadfjlsdflkjslgfs;lfjlsajfl;sajfasfd.pptx
theknightme
 
西班牙武康大学毕业证书{UCAMOfferUCAM成绩单水印}原版制作
Taqyea
 
Orchestrating things in Angular application
Peter Abraham
 
Apple_Environmental_Progress_Report_2025.pdf
yiukwong
 
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
PM200.pptxghjgfhjghjghjghjghjghjghjghjghjghj
breadpaan921
 
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
Optimization_Techniques_ML_Presentation.pptx
farispalayi
 
The-Hidden-Dangers-of-Skipping-Penetration-Testing.pdf.pdf
naksh4thra
 
Computer Securityyyyyyyy - Chapter 2.ppt
SolomonSB
 
𝐁𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓
hokimamad0
 

Mysql connection

  • 1. http://www.tutorialspoint.com/mysql/mysql-connection.htm Copyright © tutorialspoint.com MYSQL CONNECTIONMYSQL CONNECTION MySQL Connection using mysql binary: You can establish MySQL database using mysql binary at command prompt. Example: Here is a simple example to connect to MySQL server from command prompt: [root@host]# mysql -u root -p Enter password:****** This will give you mysql> command prompt where you will be able to execute any SQL command. Following is the result of above command: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 2854760 to server version: 5.0.9 Type 'help;' or 'h' for help. Type 'c' to clear the buffer. In above example, we have used root as a user but you can use any other user. Any user will be able to perform all the SQL operations, which are allowed to that user. You can disconnect from MySQL database any time using exit command at mysql> prompt. mysql> exit Bye MySQL Connection using PHP Script: PHP provides mysql_connect() function to open a database connection. This function takes five parameters and returns a MySQL link identifier on success or FALSE on failure. Syntax: connection mysql_connect(server,user,passwd,new_link,client_flag); Parameter Description server Optional - The host name running database server. If not specified, then default value is localhost:3036. user Optional - The username accessing the database. If not specified, then default is the name of the user that owns the server process. passwd Optional - The password of the user accessing the database. If not specified, then default is an empty password. new_link Optional - If a second call is made to mysql_connect() with the same arguments, no new connection will be established; instead, the identifier of the already opened connection will be returned. client_flags Optional - A combination of the following constants: MYSQL_CLIENT_SSL - Use SSL encryption MYSQL_CLIENT_COMPRESS - Use compression protocol MYSQL_CLIENT_IGNORE_SPACE - Allow space after function names
  • 2. MYSQL_CLIENT_INTERACTIVE - Allow interactive timeout seconds of inactivity before closing the connection You can disconnect from MySQL database anytime using another PHP function mysql_close(). This function takes a single parameter, which is a connection returned by mysql_connect() function. Syntax: bool mysql_close ( resource $link_identifier ); If a resource is not specified then last opened database is closed. This function returns true if it closes connection successfully otherwise it returns false. Example: Try out the following example to connect to a MySQL server: <html> <head> <title>Connecting MySQL Server</title> </head> <body> <?php $dbhost = 'localhost:3036'; $dbuser = 'guest'; $dbpass = 'guest123'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($conn); ?> </body> </html>