Add Topic


waiting answer July 28, 2021

PHP How to Connect to Mysql

My hostname is localhost, phpmyadmin username is root and password is root. I want to connect my PHP code to the 'test' database. How to connect MySQL database with PHP

February 09, 2021

The connect function in PHP opens a new connection to the MySQL server.


      mysqli_connect(hostname, username, password, database_name)

      hostname -> Specifies hostname 
      username -> Specifies the mysql username
      password -> Specifies the mysql password
      database_name -> Specifies the Database Name


      mysqli_connect("localhost", "root", "root", "test_database")

Look at example of procedural style below.

     $connect = mysqli_connect("localhost", "root", "root", "test_database");
        echo "Connected to db successfully";
        echo "Error: Unable to connect to MySQL." . PHP_EOL;
0 0
February 09, 2021

There are three types of methods in PHP to connect MySQL database. They are, 

  1.  MySQLi
  2.  MySQL
  3.  PDO

Mysqli and PDO are actively used. Mysql is obsolete due to some security issues like SQL injection.

0 0

Please Login to Post the answer

Leave an Answer