MySQL Connection

MySQL Connection

In this section, you will learn how to connect to MySQL Server using MySQL command-line and PHP.

How connect to the MySQL server from the command?

The following commands will be used to connect to the MySQL Server.

-u user name of the server that you connect to the MySQL Server as the user account root.
-p How to instruct MySQL to ask for a password.

You type the password for the user account root and press Enter:

MySQL Connection Using PHP

PHP 5 and later can work with MySQL database using: MySQLi extension (the “i” stands for improved) PDO (PHP information Objects).

Example (MySQLi Object-Oriented)

Try the following example to connect to a MySQL server in MySQLi Object-Oriented way.

Example (MySQLi Procedural)

Try the following example to connect to a MySQL server in MySQLi Procedural way.

Example (PDO)

Try the following example to connect to a MySQL server in MySQLi PDO way.

Close the Connection

The connection can be closed by using the following PHP functions.

What is Next?

In the next section, you will learn how to create a database in MySQL.

* Please do let us know your views on this article and what type of articles would you are interested to read


Widget not in any sidebars

Digiprove sealCopyright secured by Digiprove © 2020

Leave a Reply

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