Unable to connect to MySQL with PHP PDO

Website URL

http://helpdeskup.infinityfreeapp.com/

Error Message

Hi InfinityFree Support,

I am having trouble connecting to MySQL with PHP PDO on my InfinityFree account. I am using the following code:

$dbh = new PDO(mysql:host=sqlxxxx. infinityfree.com;dbname=my_user_helpdesk;port=3306, my_user, my_pass)

I have tried using both “sqlxxxx. infinityfree. com” and “sqlxxxx. byetcluster .com” as the host, and I have also tried using the username and password that I have listed in my InfinityFree dashboard. However, I am still unable to connect to the database.

Sometimes, I do not get any error message, but other times I get the following error:

PHPphp_network_getaddresses: getaddrinfo for my_user failed: Name or service not known

Other Information

(other information and details relevant to your question)

Definitely wrong. Make sure your credentials are correct

Of course I put my credentials. The “my_user” is just to protect my data.

Read again. Your dbname is wrong

I think not
my username: if0_35201982
dbname: if0_35201982_helpdesk

2 Likes

I’m not sure since you’re not sharing the actual values, but it seems you keep a space between the dot and the infinityfree.com part of the domain, as also shown here:

Would it be possible to share your actual values, redacting only the password? You’ve already shared your username and dbname, so showing the SQL URL in the code exactly as it is shouldn’t be an issue.

Take a look at this article too if you haven’t already:

Specifically this part:

5 Likes

MySQL Databases for if0_35201982

MYSQL USERNAME

if0_35201982

MYSQL PASSWORD

************ Show/Hide

MYSQL DATABASE NAME

if0_35201982_XXX
(see below)

MYSQL HOSTNAME

sql113.infinityfree.com

MYSQL PORT (OPTIONAL)

3306

$host   = "sql113.epizy.com"; 
        $bbdd   = "if0_35201982_helpdesk"; 
        $user   = "if0_35201982";
        $pass   = *******
        $puerto = '3306';
        $opcionesPDO = [];
        $driver = 'mysql';
        $dbh    = false;

        try {
            $dsn = "$driver:host=$host;dbname=$bbdd;port=$puerto";
            //$conn = mysqli_connect($user, $host, $pass, $bbdd);
            //var_dump($dsn);
            //var_dump($conn);
            $dbh = new PDO($dsn, $user, $pass);//, $opcionesPDO);

your code seems to have been cut off - I’m assuming it continues like this?

    try {
        $dsn = "$driver:host=$host;dbname=$bbdd;port=$puerto";
        $dbh = new PDO($dsn, $user, $pass);//, $opcionesPDO);
    }
    catch(PDOException $e) {
        echo $e->getMessage();
    }
1 Like

yes

when i put this code:

try {
            $dsn = "$driver:host=$host;dbname=$bbdd;port=$puerto";
            $dbh = new PDO($dsn, $user, $pass, $opcionesPDO);

            if(!$dbh){
                $dbh->setAttribute(PDO::ATTRR_ERRMODE, PDO::ERRMODE_SILENT);
                $dbh->setAttribute(PDO::ATTRR_ERRMODE, PDO::ERRMODE_WARNING);
                $dbh->setAttribute(PDO::ATTRR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                
                return $dbh;

            }else{
                var_dump($dbh);
                echo 'No se ha podido conectar';
                die();
            }
    
        } catch (PDOException $e){
            echo $e->getMessage();
            die();
        }

get me only a:
object(PDO)#2 (0) { }

enter on: if(!$dbh) else

I noticed this:

Since your domain does not start with epiz_, perhaps you want to use the infinityfree.com domain? You’ve mentioned that you tried it before, so was epizy left from a previous test?

Is it going into the “if” or the “else”?

1 Like

I tried with differents hostname, my real host in the dash is sql113.infinityfree.com
but if i put it or oder like “sqlxxxx. infinityfree. com” and “sqlxxxx. byetcluster .com” have the same problem

and going into the else

    else{
        var_dump($dbh);
        echo 'No se ha podido conectar';
        die();
    }

when i use:
$conn = mysqli_connect($user, $host, $pass, $bbdd);

get me this error:

Warning : mysqli_connect(): php_network_getaddresses: getaddrinfo for if0_35201982 failed: Name or service not known in

Format this part like this
```
Test
```
Also remove that space from the value of the host variable.

2 Likes

This sounds to me like your code is trying to use the username as the database hostname. Please make sure to place the MySQLi connect parameters in the right order. It’s supposed to be $hostname, $username, $password, $database.

https://www.php.net/manual/en/mysqli.construct.php

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.