简体   繁体   中英

How do I connect my PHP file to an external database?

I was wondering if I may get some help with trying to connect to an external database for one of my PHP config files. This PHP file is from a plugin I downloaded from cartmega, which is supposed to tie-in with our ticketing system called osTicket. When we connect to our DB, data is supposed to be fetched from one of the tables via a protected function and a few commands that were already on the config file.

Now I'm able to connect to my local database, however, I'm only able to connect to that local DB using localhost. I've tried using the hostname of the local server, but for some reason it wasn't pulling the data from my mariadb table structure. I need both the local and external databases because both DB's have different data that needs to populate on our ticketing system.

This is how the connection code is currently setup

protected $parent
private $_db

public function_construct($parent) {
    $this->parent = $parent;
    $this->_db = mysqli_connect("localhost", "username", "password", "osticket_db")

Now my thinking is that if I want to connect to an external db, then I should do the following

protected $parent
private $_db
private $_db2

public function_construct($parent) {
    $this->parent = $parent;
    $this->_db = mysqli_connect("localhost", "username", "password", "osticket_db")
    $this->_db2 = mysqli_connect("name.name.com", "username", "password", "snipeIT")

So thanks for your guidance everyone. It gave me an idea for what to look for, so I managed to figure it out. I had to grant access to the sql server by granting access to the sql user that is trying to log into the external server. After that I edited the sql .cnf file and added a bind address, once I completed that I tried to allow the ports again, and then ran a few iptables command to allow the IP and port that is trying to access the localserver and external server. After that I was able to connect to the external database from the local database.

Thanks again!

You need to put IP of the external database instead of localhost and you need to allow remote access from the outside database to grant the access. Remote access can be accessed from cPanel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM