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

PPTX
Activemq installation and master slave setup using shared broker data
PDF
Mysql administration
PDF
How to setup and connect my sql to ec2 instance from ubuntu
PPTX
Mule Clustering
PPT
Core data optimization
PPT
mysql-win.ppt
PPT
My SQl
PPT
Raj mysql
Activemq installation and master slave setup using shared broker data
Mysql administration
How to setup and connect my sql to ec2 instance from ubuntu
Mule Clustering
Core data optimization
mysql-win.ppt
My SQl
Raj mysql

What's hot (13)

PPTX
Enabling Security For ActiveMQ JMX Access
ODP
Sql installation
PDF
Using XMPP JSONPatch for synchronizing an OpenUI5 Model
PPTX
Compress and decompress
PPTX
Caching and invalidating with managed store
PPTX
Caching and invalidating with managed store
 
PPSX
Execute sql query or sql command sql server using command prompt
PPTX
Splitting with mule part2
PPTX
Quartz in mule
PPTX
Mule idempotent filter and Object Store
PPTX
NodeJs Session02
DOCX
Query transport problems
PPTX
Caching and invalidating with managed store
Enabling Security For ActiveMQ JMX Access
Sql installation
Using XMPP JSONPatch for synchronizing an OpenUI5 Model
Compress and decompress
Caching and invalidating with managed store
Caching and invalidating with managed store
 
Execute sql query or sql command sql server using command prompt
Splitting with mule part2
Quartz in mule
Mule idempotent filter and Object Store
NodeJs Session02
Query transport problems
Caching and invalidating with managed store
Ad

Viewers also liked (8)

PPT
Five Common Challenges in Blogging and How to Overcome Them
PDF
Tabison company profile 2017
PPT
Should Journalists Blog?
PDF
Mysql alter-command
PDF
Maggie Q
PPTX
role of agricultural policies in responding to food security in India
PDF
Overture - Bjork
PDF
Opendata and business - داده های باز و کسب و کار
Five Common Challenges in Blogging and How to Overcome Them
Tabison company profile 2017
Should Journalists Blog?
Mysql alter-command
Maggie Q
role of agricultural policies in responding to food security in India
Overture - Bjork
Opendata and business - داده های باز و کسب و کار
Ad

Similar to Mysql connection (20)

PPTX
3-Chapter-Edit.pptx debre tabour university
PPTX
chapter_Seven Database manipulation using php.pptx
PDF
Python my sql database connection
DOCX
Php mysql connectivity
PPTX
My sql administration
PDF
PHP with MySQL
PDF
Instalar MySQL CentOS
PDF
SULTHAN's PHP, MySQL & wordpress
PPT
Mysql ppt
PDF
PHP and Mysql
PPTX
Php mysq
PDF
My sql università di enna a.a. 2005-06
PPTX
Database Connectivity in PHP
PPTX
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
PPT
Mysqlppt3510
PPT
Mysqlppt3510
PPT
PHP - Getting good with MySQL part II
PDF
Pydbapi
PPT
Php with MYSQL Database
PDF
Mysql tutorial 5257
3-Chapter-Edit.pptx debre tabour university
chapter_Seven Database manipulation using php.pptx
Python my sql database connection
Php mysql connectivity
My sql administration
PHP with MySQL
Instalar MySQL CentOS
SULTHAN's PHP, MySQL & wordpress
Mysql ppt
PHP and Mysql
Php mysq
My sql università di enna a.a. 2005-06
Database Connectivity in PHP
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
Mysqlppt3510
Mysqlppt3510
PHP - Getting good with MySQL part II
Pydbapi
Php with MYSQL Database
Mysql tutorial 5257

Recently uploaded (20)

PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PPTX
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
PDF
How to Ensure Data Integrity During Shopify Migration_ Best Practices for Sec...
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PPTX
Funds Management Learning Material for Beg
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PPTX
Introduction to Information and Communication Technology
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
DOCX
Unit-3 cyber security network security of internet system
PDF
Introduction to the IoT system, how the IoT system works
PPTX
SAP Ariba Sourcing PPT for learning material
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
PDF
The Internet -By the Numbers, Sri Lanka Edition
Job_Card_System_Styled_lorem_ipsum_.pptx
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
Slides PDF The World Game (s) Eco Economic Epochs.pdf
522797556-Unit-2-Temperature-measurement-1-1.pptx
How to Ensure Data Integrity During Shopify Migration_ Best Practices for Sec...
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
Funds Management Learning Material for Beg
SASE Traffic Flow - ZTNA Connector-1.pdf
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
Cloud-Scale Log Monitoring _ Datadog.pdf
Tenda Login Guide: Access Your Router in 5 Easy Steps
Introuction about WHO-FIC in ICD-10.pptx
Introduction to Information and Communication Technology
Slides PPTX World Game (s) Eco Economic Epochs.pptx
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
Unit-3 cyber security network security of internet system
Introduction to the IoT system, how the IoT system works
SAP Ariba Sourcing PPT for learning material
RPKI Status Update, presented by Makito Lay at IDNOG 10
The Internet -By the Numbers, Sri Lanka Edition

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>