subless
November 3, 2017, 12:51pm
#1
I cannot get my code to connect to MySQL on this site, but I can connect to MySQL just fine on my local XAMPP instance. I’ve used the credentials my account gave me but still nothing. Below is the code I use and it works for the latest version of MySQL and PHP on XAMPP just not online here.
Any suggestions?
// Create connection
$conn = new mysqli("localhost", "idxxxxxxx_user", "xxxxxxxx", "idxxxxxxx_db");
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
EDIT:
Here’s the error code:
Connection failed: ProxySQL Error: Access denied for user ‘idxxxxxxx_user’@‘2a02:4780:bad:f00d::f’ (using password: YES)
Obviously, I have removed my real username, password, and database names for this post.
Are you using the code on your 000webhostapp URL?
If you are trying a script locally to connect to 000webhost MySQL it won’t work.
(you probably know this but I have to say it )
subless
November 3, 2017, 3:27pm
#5
No. Locally I’m using XAMPP with my local MySQL db and everything works fine. But when I uploaded all my code to 000WebHost.com and created my db and received my new credentials I updated my code’s credentials to reflect the new ones. But it will not connect and I cannot figure out why.
Do you specify anything about ipv4/6 in your script?
subless
November 3, 2017, 3:28pm
#7
No. I use the code I posted above to test the db connection.
Method 1 — Connecting to the database using PDO (recommended)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database = "database";
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
Method 2 — Connecting to the database using mysqli (Object-Oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Method 3 — Connecting to the database using mysqli_connect (Procedural)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database = "database";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
subless
November 3, 2017, 3:38pm
#9
I was already using Method 2, I tried method 1 and 3 and still getting the same error.
What is your 000webhostapp URL?
subless
November 3, 2017, 3:51pm
#11
https://mycardatabase.000webhostapp.com/
Just type anything in the login field and click login. It’s a fake login page for now until I get get the db connection working. The error will be displayed at the bottom after you login. It’s for my school project.
Wrong password in config file.
Head to 000webhost.com > Manage > Change Password > set it to the one within config file else make a new password and replicate this into the config file.
subless
November 3, 2017, 3:57pm
#13
I changed it, and updated my config file but it still says the same thing.
EDIT: It took a few minutes after I changed my password for the changes to reflect, but it’s working now. Thank you so much!
Should work fine now, try the first method above.
1 Like
subless
November 3, 2017, 4:01pm
#15
It took a few minutes after I changed my password for the changes to reflect, but it’s working now. Thank you so much!
1 Like
No worries, happy site building
1 Like