Menu

Add Topic

Forum

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

Answers
February 09, 2021

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

Syntax:

      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

Example:

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

Look at example of procedural style below.

     <?php
     $connect = mysqli_connect("localhost", "root", "root", "test_database");
     if($connect){
        echo "Connected to db successfully";
     }else{
        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