Hostgator remotely access database in local php file - php

I am currently using HostGator but my problem is I cannot connect to my online database (which is the HostGator) using my localhost PHP file. I want that if I have internet connection I will connect to my online database but if none I will connect to my offline localhost database. Here's my code:
$checknet = #fsockopen("www.mysite.com", 80);
if(isset($checknet)) {
mysql_connect('mysite.com OR ip of mysite','username','password');
mysql_select_db('db');
} else {
mysql_connect('localhost','root','');
mysql_select_db('db');
}
Also, I already added my IP address to the access whitelist of the database. But, accessing the database from a remote system still doesn't work.

You'll have to whitelist your public IP address.
a) Google "what is my ip"
b) Follow the tutorial on this link.

Related

Connect remotely to cpanel Database with php

I am trying to access to the database of my live website from localhost. My website is working on cpanel hosting.
I created database and added user to that database but can not connect to this database from my localhost .
this is the code of database information
<?php
$conf['dbuser']='admin_root';
$conf['dbpass']='rootroot!##$';
$conf['dbname']='bsaiiian_os';
$conf['dbhost']='example.com';
$conf["title"]='DEMO';
?>
I think the problem is from $conf['dbhost'] variable where I'm putting the url of my site and not sure if that is right.
When I'm uploading my script to the hosting and put
$conf['dbhost']='localhost';
the connection is established .
this is error message:
Connection failed: No connection could be made because the target machine actively refused it.
Due to security reasons cPanel's MySQL requires that you allow your IP in order to have access.
This is done through:
cPanel -> Remote MySQL Connection
You need to navigate there and allow your local computer's IP in order to make the connection.
Side Note: This stands true for all IPs, even the server's one that your account is hosted on. You will see your cPanel account's IP already present in there - this is to allow access to MySQL for any files from your cPanel account

Remote SQL connection: mysqli_connect(): (HY000/1045): Access denied for user

I have a database that I want to transfer to my remote server from localhost. The file being too big, I wrote the code to copy the table. But the problem the connection is not successful. My code looks like this.
$server_conn=mysqli_connect($db_host,$db_user,$db_pass,$db_name);
if (!$server_conn) {
die("Server Connection Fail: " . mysqli_connect_error());
}
Where $db_host is the ip address of my server.
When I load the script the error I get is
Server Connection Fail: Access denied for user db_user#'117.202.126.83' (using password: YES)
The credentials are correct, the host is correct, and the weirdest part is 117.202.126.83 is my IP address.
I don't know how is it ignoring the host and taking my connection IP address for host. Even when I give the db_host as the IP address of my remote server.
Also, I am running the script using Easy PHP 14.1 running APACHE 2.4.7,MYSQL 5.6.15 and PHP 5.4.24.
What am I missing?
UPDATE: I am not sure if I was stupid or if the hostgator instructions were.
They ask us to add the cpanel username followed by an underscore before the database name and database username.
In the cpanel, the cpanel username is prefixed by force to all user and db names. So in my case, I had the variables as cpanelUsername_cpanelUsername_databaseName and so on.
Thank you guys for your replies
Go to the cpanel where your phpMyAdmin has the access.
Click on Remote MySQL
Add the access host (Your local IP which is running the script)
Then you can get access to remote mysql server.
You can then test the connection using MySQL Workbench ()
are you sure you use the proper external database hostname/ip?

How to make database connection from different website

I have a database in a website called booksiders.com and then I have a new website called kryptotech.co.in in which i am looking to use the database of the first website. So how can connect with the database which exists on another hosting server.
This is the code i am using in kryptotech.co.in for connecting to the database of booksiders.com from kryptotech.co.in which is hosted on another server.
<?php
$con = mysql_connect("booksiders.com","bookside","******");
if(!mysql_select_db("bookside_kryptoDB",$con))
echo "Unable to connect to database";
?>
Please tell me what hostname should be used for doing this.
You will have to allow your server IP in allow remote host access list on remote server and ask them to allow port 3306 in their firewall so that you can connect mysql databases from your hosting server.

Remote MySQL connection on a https site php?

I have recently installed a SSL certificate on my website.
This has caused some issues which I was not expecting at all.
The issue that I have right now is that I cannot connect to the MySQL database!
My connection file is like this:
<?php
$db_conx = mysqli_connect("Mydomain.com", "mystore", "Mypassword", "mystore");
// Evaluate the connection
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
} else {
echo "";
}
?>
The code above was working perfectly fine before I installed the SSL but it has stopped connecting to the MySQL database since I installed the SSL.
if I change the
$db_conx = mysqli_connect("Mydomain.com", "mystore", "Mypassword", "mystore");
to
$db_conx = mysqli_connect("localhost", "mystore", "Mypassword", "mystore");
It starts working again but I need to use the domain name same as before as I will need it to connect to a remote MySQL database.
Once again, this was working just fine before I installed the SSL.
Could someone please advise on this and what needs to be done to resolve this issue?
Thanks
try the connection with port
$db_conx = mysqli_connect("Mydomain.com:3306", "mystore", "Mypassword", "mystore");
Your hosting company might have changed/added IP address(es) to your machine, as traditionally certificates demanded their own IP address. Usually a hosting company would just add an extra IP address to your web server machine, on top of the IP address you already had.
Now if your PHP script uses the new IP address to connect to the MySQL server, your MySQL server might not recognize the new IP, and reject the connection.
Ask your hosting company to either set the former IP address as the one that PHP uses to connect to MySQL. Or ask them to set a route to the MySQL server via the former IP.
Or you can just check if you have enough privileges to change the IP address that the MySQL server grants access to.

Connecting Local Host to online sql database

I have an online reservation booking system (php script) that uses a mySQL database. Part of the script is a back end admin panel for offline reservations.
Is it possible to run a service like XAMPP using local host to access the remote database.
You can connect to remote database by having remote host, db username, db password.
Host name = (use the db server IP address)
Database name = (cpanelUsername_databaseName)
Database username = (cpanelUsername_databaseUsername)
Database password = (*)
MySQL Connection Port = 3306 (check your)
If you are using cPanel then you need to allow your local IP for DB access first.
Login to cpanel, on the main menu of CPanel,
Jump down to "Databases" and select "Remote MySQL"
Now add your IP address, or IP range with a wildcard such as 12.34.%
Good Luck
Yes. You need login credentials for the remote database as well as it's port and server address (hostname or IP), as you'd expect. The remote host also needs to have permissions granted for that particular user to access the database remotely, which users will not have by default.
Yes it is. Just the way you connect your local database, you can connect to a remote database with valid credentials. Then only thing changes is the hostname parameter.
for an example if your database is hosted on dreamhost, they have hostnames like..mysql.yourdomain.com. Create a database on your remote server and connect to it as
<?php
mysql_connect('mysql.yourdomain.com','username','password');

Categories