I cannot connect MySQL web server - php

I added a database to my cPanel, but I cannot connect it.
try{
$db = new
PDO('mysql:host=markus.veridyen.com;dbname=sosyalki_pratiki1_ipss;charset=utf8','sosyalki','password');
}catch(PDOException $e){
echo 'Hata: '.$e->getMessage();
}
When I try to connect my database, it gives me this error message:
Hata: SQLSTATE[HY000] [2002] The connection could not be established because the target machine actively refused.
Notice: Undefined variable: db in C:\xampp\htdocs\ipss\survey.php on line 55

You still can use localhost as server name on a live server.
I'll assume you are using cpanel.
<?php
$servername = "localhost";
$username = "username"; //your cpanel username
$password = "password"; //your cpanel password
try {
$conn = new PDO("mysql:host=$servername;dbname=DatabaseName", $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();
}
?>

Related

What is the issue in this PHP code to connect on localhost XAMPP

The error is:
Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'moomen'#'localhost' (using password: YES)
Code is:
<?php
$servername = "localhost";
$username = "moomen";
$password = "9124279123";
$dbname = "ecocaa";
try {
$conn = new PDO("mysql:host=$servername;dbname=$servername", $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();
}
I can see a problem in your code
In the dbname you have used server name, change the code it will work.

Stuck with connectivity PHP and MySQL in rhel 7.3

I am working on PHP and MySQL environment in rhel7.3. And I got another problem with my connectivity. I am unable to create a connection between both of them.
i got the error.
Connection failed: SQLSTATE[HY000] [2059] Authentication plugin
'caching_sha2_password' cannot be loaded:
/usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared
object file: No such file or directory .
And code is as follow
<?php
$servername = "localhost";
$username = "root";
password = "Gstadmin#123";
try {
$conn = new PDO("mysql:host=$servername;dbname=test", $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();
}
?>

Connection Refused on connecting to a remote server

I'm new to web development. I bought a server on DigitalOcean, created a droplet and installed LAMP. I have a simple PHP script to connect to MySQL. Here's my PHP script(conn.php)
<?php
$servername = "xxx.xx.xx.xxx";
$username = "root";
$password = "my_password";
try {
$conn = new PDO("mysql:host=$servername;dbname=mysql", $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();
}
?>
Now, when I try to run on my local "xxx.xx.xx.xxx/conn.php", it's printing
Connection failed: SQLSTATE[HY000] [2002] Connection refused

Phpmyadmin in remote pc is accessible, but can't connect it with PHP PDO

I'm able to connect phymyadmin of remote pc, but when i try to connect to remote db, I'm getting connection refused error.
I have seen similar kind of question, but not yet answered also it is not active now.SQLSTATE[HY000] [2002] Connection refused with right port
<?php
$servername = "192.168.1.12";
$username = "root";
$password = "root";
try {
$conn = new \PDO("mysql:host=$servername;dbname=my_db", $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();
}
?>
In remote pc, bind address in mysql configuration file was set to 127.0.0.1 instead of 192.168.1.12

SQLSTATE[HY000] [2002] Connection refused

I am trying to create a simple mailing list webpage. Using LAMP. Here is the code I have for connecting to my database:
<?php
function insert()
{
$servername = "127.0.0.1";
$username = "root";
$password = "(my password here)";
$dbname = "(my db name here)";
try
{
// preparing database handle $dbh
$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo ("Could not connect to server.\n");
echo ("getMessage(): " . $e->getMessage () . "\n");
}
}
?>
The function continues after that but this connection attempt gets cung up on the catch, and throws the "SQLSTATE[HY000] [2002] Connection refused" error. I have tried:
-Changing the $servername to localhost, but this gives me a different error: "SQLSTATE[HY000] [2002] No such file or directory"
-trying to specify all different ports
-checked all my info, database name and password.
-I can log into phpmyadmin and see that my database is fine
-looked at all other questions on this topic, with no help found.

Categories