Connect MySQL Database with PHP Script and Command Line

Connect MySQL Database with PHP Script and Command Line

To connect to mysql database server using command line or PHP code; In this article, You will learn how to connect mysql using command line (CMD) and also learn how to connect MySQL using php script.

You can use the MySQL command to quick and easy way to access your databases directly.

How to connect to MySQL Database using Command Line and PHP Script/Code

There are two simple ways to connect mysql database using cmd or php code; as shown below:

  • MySQL Connect Using CMD or Command Line
  • PHP Database Connection with MySQL

MySQL Connect Using CMD or Command Line

Steps to connect MySQL from using command line:

Log in to your Hosting account using SSH.

On the command line, write the following command, replace USERNAME to your USERNAME :-

mysql -u USERNAME -p

Enter Password

At the password prompt, type your password. When you type the correct password, mysql> prompts are displayed.

To See List of Database

To display a list of databases, Use the below command at the mysql> prompt :-

show databases;

Use Database

To access a specific database, use the following command at the mysql> prompt, and also replace the DBNAME with the database you your database name :-

use DBNAME;

Accessed Database

After accessing the database, you can run SQL queries, list tables and so on. apart from this :-

  • To see a list of MySQL commands, type help at mysql> prompt.
  • To exit the Mysql program, type \ q at mysql> prompt.

PHP Database Connection with MySQL

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

mysql_connect(host,username,password,new_link,client_flag);

Host

The host name that runs the database server If not specified, the default value will be localhost.

username

Username accessing the database If not specified, the default will be the name of the user who is the owner of the server process.

password

The default value is defined by mysql.default_password. In SQL safe mode, this parameter is ignored and blank password is used.

new_link

If mysql_connect () is called with other arguments, then no new connection will be established; Instead, the identifier of the previously open connection will be returned.

client_flags

  • MYSQL_CLIENT_SSL – Use SSL encryption.
  • MYSQL_CLIENT_COMPRESS – Use Compression Protocol.
  • MYSQL_CLIENT_IGNORE_SPACE – Allow the location after the function names.
  • MYSQL_CLIENT_INTERACTIVE – Allow passive seconds of idle time before closing the connection.
  • Example Of MySQL Connection

Example of connection to a MySQL Database

Here is an example of database connection in php with mysql:

<?php
$host= "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = mysqli_connect($host, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo 'Connected successfully';

mysqli_close($conn);
?>

Recommended Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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