0

I created a php website and host it online by using Heroku recently. However, I couldn't connect my database because I use localhost phpmyadmin database from my online site. How should I connect my localhost database from my online site? Or how to make my host my database online, and then make the connection? Here is my db.php file to make the connection with mysqli, in case if there's any need to tweak the db connection PHP codes.

//step 1: Establish database connection
DEFINE("DB_HOST", 'localhost');
DEFINE("DB_USER", 'root');
DEFINE("DB_PASS", '');
DEFINE("DB_NAME", 'my_db_name');
// Create connection
$con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Set charset to UFT8
mysqli_set_charset($con, "utf8");
// Check connection
if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}
  • you can migrate local db to hosting and add ssl layer as well for security reason . db.php connection file seems good , it will work , i would also suggest use any php framework like laravel,codignator etc. – ANISH KUMAR MOURYA Sep 02 '20 at 07:14

1 Answers1

0

If you are hosting your database on your local machine, whilst having a webserver that needs to connect to your database, you will have to make sure your database can be accessed by another machine. The host is only ‘localhost’ if the Local IP is identical for the machine and database. Your db.php is correct and will function properly, as long as you ensure that the connection details are correct, since localhost is not correct there will be errors.

You might want to look into how to portforward depending on what database software/installation you are using. Or look into whether your host offers database solutions sch as or similar to PhpMyAdmin (in case of most webhosts).

GodLess
  • 48
  • 8
  • I checked some online tutorials and come across clearDB in Heroku and MySQL workbench. However, after the installation of the add-ons and software, creating users and passwords, I don't know how to make the connection in my db.php because the online tutorial didn't show it. I checked many tutorials but to no avail. – ShenLoong Sky Sep 02 '20 at 08:38
  • https://stackoverflow.com/questions/9822313/remote-connect-to-cleardb-heroku-database If you are using ClearDB, you can find out how to establish a remote connection by reading the answer to the question here – GodLess Sep 02 '20 at 08:58