MySQL Connection

Connecting to MySQL

Before you perform any operations, you need to connect to MySQL server and select a MySQL database.

Connecting to MySQL server from command line

Command:MySQL -h HostName -u UserName -p

Parameters:

NameDescription
-hKeyword, followed by HOSTNAME.
HostNameMySQL server name
-uKeyword, followed by USERNAME.
UserNameMySQL user name
-pAsks you to enter a password.

As soon as you enter this command, it asks you to provide a password for the user you mentioned. Supplying the appropriate password would allow you to connect to MySQL server.

Connecting to MySQL database from command line

Command:use DatabaseName;

Where DatabaseName is the database you want to select.

Disconnecting or closing MySQL server from command line

Command:exit

This command must be executed from the MySQL prompt.

Connecting to MySQL server and database using PHP

<?php
$host="localhost";
$username="root";
$password="";
$db_name="bookinfo";
$con=MySQL_connect("$host", "$username", "$password")or die("cannot connect");
MySQL_select_db("$db_name")or die("cannot select DB");
?>

Copy

Replace the values of the $host, $username, $password and $db_name according to your own setup. Notice that we have also selected the database with PHP, which is a must before you can fetch data from a MySQL database.

Disconnecting or closing a MySQL connection using PHP

<?php
MySQL_close($con);
?>

Copy

Where $con=MySQL_connect(“$host”, “$username”, “$password”)or die(“cannot connect”), shown in the previous example.

https://www.adsensecustomsearchads.com/afs/ads?psid=5134551505&channel=AutoRsVariant&cx=r-440389826592af9d2&fexp=21404%2C17301374%2C17301375%2C17301383%2C71847096&client=pub-2153208817642134&r=m&sct=ID%3Df1c5d672aa31266c%3AT%3D1706354011%3ART%3D1706354011%3AS%3DALNI_MZlJMMg_q5l3r_1tPiuFzhttpHLOQ&sc_status=6&hl=en&rpbu=http%3A%2F%2Fgoogle.com&rpqp=q&type=3&rs_tt=c&oe=UTF-8&ie=UTF-8&format=r5&nocache=8531706402036696&num=0&output=afd_ads&domain_name=www.w3resource.com&v=3&bsl=10&pac=0&u_his=5&u_tz=-480&dt=1706402036701&u_w=1366&u_h=768&biw=1297&bih=644&psw=1297&psh=644&frm=0&cl=600476684&uio=-&cont=autors-container-0&drt=0&jsid=csa&jsv=600476684&rurl=https%3A%2F%2Fwww.w3resource.com%2Fmysql%2Fmysql-connection.php&referer=https%3A%2F%2Fwww.w3resource.com%2Fmysql%2Fmysql-language-structure.php

Note: It will be convenient for you if you keep the script you require to connect to and disconnect from MySQL in a PHP file and then include that file in each PHP file you are using to perform various operations on the database.

Using MySQL persistent connection with PHP

Description:

1. MySQL persistent connection is a connection which first tries to find if any identical (i.e. with the same hostname, username, and password) exists. If so, then commands followed will use that connection. If such a connection does not exist, it would create one.

2. MySQL persistent connection does not need a MySQL_close().

PHP code:

<?php
$host="localhost";
$username="root";
$password="";
$db_name="bookinfo";
$con=MySQL_pconnect("$host", "$username", "$password")or die("cannot connect"); MySQL_select_db("$db_name")or die("cannot select DB");
?>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *