How to connect to a MySQL database using PHP

The hostname to use when connecting to one of your cPanel MySQL databases from a web script running within the same cPanel account is localhost.

As a quick guide, you can use the following PHP code to make a connection.

$hostname = "localhost"; // mysql hostname - always use localhost
$database = "databasename"; // substitute your mysql database name
$username = "username"; // substitute your mysql user name
$password = "somethinghardtoguess"; // your mysql users's password
$con = mysqli_connect($hostname, $username, $password, $database) or die(mysql_error()); // make connection to mysql


How did we do?


Powered by HelpDocs (opens in a new tab)
© Krystal Hosting Ltd 2002–