I am not able to connect to MySQL database from PHP? - Cloud


Using cloud hosting for a website is one of the ways where you will get a lot of benefits from it. And today switching your business to cloud server will improve your company's workflow and also save money and also ensure that you stay ahead of all the curves. Moreover, cloud computing is the practice of using a network of remote server which is hosted on the internet store, manage and process the data. We can say that cloud server is the way of the future and using cloud server it will help your business to go in the right direction and it will also give you an idea on how you can run your business. Today cloud server is a powerhouse which is built to run any businesses effectively and efficiently as per it situation or environment.


And today if you are using a cloud server hosting for your website and if you are looking for an information on how to connect to MySQL database from PHP on your cloud server then here is the complete solution on it. The reason why you cannot connect to your MySQL database from PHP is that you might have given the wrong hostname. So always be careful when you enter the hostname. And the hostname is- localhost. And here requirements that you need before you start- Host: localhost, username: your username, password: your password, database: your database name.


Sample connection script


Here is the sample PHP connection script which shows three different methods of connecting to MySQL. Below is the example of sample PHP connection script and you have to replace the arguments with your own MySQL login credentials.



// mysqli

$mysqli = new mysqli("example.com", "user", "password", "database");

$result = $mysqli->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");

$row = $result->fetch_assoc();

echo htmlentities($row['_message']);


// PDO

$pdo = new PDO('mysql:host=example.com;dbname=database', 'user', 'password');

$statement = $pdo->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");

$row = $statement->fetch(PDO::FETCH_ASSOC);

echo htmlentities($row['_message']);


// mysql

$c = mysql_connect("example.com", "user", "password");


$result = mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");

$row = mysql_fetch_assoc($result);

echo htmlentities($row['_message']);


So currently if you are having a website and now if you are looking to switch to cloud server then think about HostingRaja. Yes, here at HostingRaja we provide best in class cloud servers for our customers at an affordable price. Not only that when you buy cloud servers from us you also get amazing offers, features, and discounts. Moreover, HostingRaja is hugely popular in India, especially amongst the technical and web development community. Not only that we use the latest & advanced hardware for the dedicated servers. And we offer storage based on SSD and use advanced cloud technologies to create cloud instances.

We also provide 99.99% server uptime with 24/7 customer support via phone, chat, email, and ticket system, so you can contact us any time and our customer support team will resolve your issues quickly as possible.