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
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
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
There are three types of methods in PHP to connect MySQL database. They are,
Mysqli and PDO are actively used. Mysql is obsolete due to some security issues like SQL injection.
0 0Please Login to Post the answer