# Unable to connect to MySQL with PHP PDO

**URL:** https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225
**Category:** Hosting Support
**Created:** [October 11, 2023, 11:38am UTC](https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225 "2023-10-11T11:38:23Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![yosoymitxel](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/yosoymitxel/32/41672_2.png) [@yosoymitxel](https://forum.infinityfree.com/u/yosoymitxel)
#### Post date: [October 11, 2023, 11:38am UTC](https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225/1 "2023-10-11T11:38:23Z")

</div>

### Website URL

[http://helpdeskup.infinityfreeapp.com/](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](http://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)

---

<div class="post-metadata">

### Author: ![KangJL](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/kangjl/32/12130_2.png) [@KangJL](https://forum.infinityfree.com/u/KangJL)
#### Post date: [October 11, 2023, 12:12pm UTC](https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225/2 "2023-10-11T12:12:54Z")

</div>

> [@yosoymitxel](#):
>
> dbname=my\_user\_helpdesk

Definitely wrong. Make sure your credentials are correct

---

<div class="post-metadata">

### Author: ![yosoymitxel](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/yosoymitxel/32/41672_2.png) [@yosoymitxel](https://forum.infinityfree.com/u/yosoymitxel)
#### Post date: [October 11, 2023, 12:14pm UTC](https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225/3 "2023-10-11T12:14:22Z")

</div>

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

---

<div class="post-metadata">

### Author: ![KangJL](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/kangjl/32/12130_2.png) [@KangJL](https://forum.infinityfree.com/u/KangJL)
#### Post date: [October 11, 2023, 12:18pm UTC](https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225/4 "2023-10-11T12:18:05Z")

</div>

Read again. Your dbname is wrong

---

<div class="post-metadata">

### Author: ![yosoymitxel](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/yosoymitxel/32/41672_2.png) [@yosoymitxel](https://forum.infinityfree.com/u/yosoymitxel)
#### Post date: [October 11, 2023, 12:21pm UTC](https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225/5 "2023-10-11T12:21:07Z")

</div>

I think not  
my username: if0\_35201982  
dbname: if0\_35201982\_helpdesk

---

<div class="post-metadata">

### Author: ![ChrisPAR](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/chrispar/32/8743_2.png) [@ChrisPAR](https://forum.infinityfree.com/u/ChrisPAR)
#### Post date: [October 11, 2023, 12:32pm UTC](https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225/6 "2023-10-11T12:32:05Z")

</div>

> [@yosoymitxel](#):
>
> $dbh = new PDO(mysql:host=sqlxxxx. [infinityfree.com](http://infinityfree.com);dbname=my\_user\_helpdesk;port=3306, my\_user, my\_pass)

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](http://infinityfree.com) part of the domain, as also shown here:

> [@yosoymitxel](#):
>
> sqlxxxx. infinityfree. com” and “sqlxxxx. byetcluster .com

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:

> [@Common MySQL Connection Errors](https://forum.infinityfree.com/t/common-mysql-connection-errors/49338):
>
> Many websites use MySQL databases to store information and settings. However, there are some reasons why the connection to the database may fail. This article describes some commonly seen error messages when connecting to a database, as well as some general tips to keep in mind when setting up a database connection. Common Error Messages Using MySQL is great, but the error message returned if there is an issue logging in to your database can be quite confusing. Here are some commonly seen error…

Specifically this part:

> [@Common MySQL Connection Errors](https://forum.infinityfree.com/t/common-mysql-connection-errors/49338/1):
>
> Using the PDO library should throw a `PDOException` which you can print for more information.

---

<div class="post-metadata">

### Author: ![yosoymitxel](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/yosoymitxel/32/41672_2.png) [@yosoymitxel](https://forum.infinityfree.com/u/yosoymitxel)
#### Post date: [October 11, 2023, 12:39pm UTC](https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225/7 "2023-10-11T12:39:03Z")

</div>

### 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](http://sql113.infinityfree.com)

MYSQL PORT (OPTIONAL)

3306

```auto
$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);

```

---

<div class="post-metadata">

### Author: ![ChrisPAR](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/chrispar/32/8743_2.png) [@ChrisPAR](https://forum.infinityfree.com/u/ChrisPAR)
#### Post date: [October 11, 2023, 12:43pm UTC](https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225/8 "2023-10-11T12:43:30Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![yosoymitxel](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/yosoymitxel/32/41672_2.png) [@yosoymitxel](https://forum.infinityfree.com/u/yosoymitxel)
#### Post date: [October 11, 2023, 12:44pm UTC](https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225/9 "2023-10-11T12:44:25Z")

</div>

yes

---

<div class="post-metadata">

### Author: ![yosoymitxel](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/yosoymitxel/32/41672_2.png) [@yosoymitxel](https://forum.infinityfree.com/u/yosoymitxel)
#### Post date: [October 11, 2023, 12:46pm UTC](https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225/10 "2023-10-11T12:46:01Z")

</div>

when i put this code:

```auto
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

---

<div class="post-metadata">

### Author: ![ChrisPAR](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/chrispar/32/8743_2.png) [@ChrisPAR](https://forum.infinityfree.com/u/ChrisPAR)
#### Post date: [October 11, 2023, 12:48pm UTC](https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225/11 "2023-10-11T12:48:31Z")

</div>

I noticed this:

> [@yosoymitxel](#):
>
> MYSQL HOSTNAME
> 
> [sql113.infinityfree.com](http://sql113.infinityfree.com)

> [@yosoymitxel](#):
>
> $host = “[sql113.epizy.com](http://sql113.epizy.com)”;

Since your domain does not start with epiz\_, perhaps you want to use the [infinityfree.com](http://infinityfree.com) domain? You’ve mentioned that you tried it before, so was epizy left from a previous test?

> [@yosoymitxel](#):
>
> when i put this code:

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

---

<div class="post-metadata">

### Author: ![yosoymitxel](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/yosoymitxel/32/41672_2.png) [@yosoymitxel](https://forum.infinityfree.com/u/yosoymitxel)
#### Post date: [October 11, 2023, 12:56pm UTC](https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225/12 "2023-10-11T12:56:00Z")

</div>

I tried with differents hostname, my real host in the dash is [sql113.infinityfree.com](http://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();
    }

```

---

<div class="post-metadata">

### Author: ![yosoymitxel](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/yosoymitxel/32/41672_2.png) [@yosoymitxel](https://forum.infinityfree.com/u/yosoymitxel)
#### Post date: [October 11, 2023, 12:59pm UTC](https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225/13 "2023-10-11T12:59:56Z")

</div>

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

---

<div class="post-metadata">

### Author: ![anon42008019](https://forum.infinityfree.com/letter_avatar/anon42008019/32/5_5575768a8748004e209b776fc1b2916d.png) [@anon42008019](https://forum.infinityfree.com/u/anon42008019)
#### Post date: [October 11, 2023, 1:15pm UTC](https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225/14 "2023-10-11T13:15:28Z")

</div>

> [@yosoymitxel](#):
>
> $host = “[sql113.epizy.com](http://sql113.epizy.com)”;  
> $bbdd = “if0\_35201982\_helpdesk”;  
> $user = “if0\_35201982”;  
> $pass = \*\*\*\*\*\*\*  
> $puerto = ‘3306’;

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

---

<div class="post-metadata">

### Author: ![Admin](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/admin/32/36437_2.png) [@Admin](https://forum.infinityfree.com/u/Admin)
#### Post date: [October 11, 2023, 1:46pm UTC](https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225/15 "2023-10-11T13:46:38Z")

</div>

> [@yosoymitxel](#):
>
> getaddrinfo for if0\_35201982 failed

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](https://www.php.net/manual/en/mysqli.construct.php)

---

<div class="post-metadata">

### Author: ![system](https://infinityfree-forum-uploads.s3.dualstack.eu-central-1.amazonaws.com/original/3X/b/0/b024fd3fa88bae093c9576e55c2c94e33f890206.svg) [@system](https://forum.infinityfree.com/u/system)
#### Post date: [October 18, 2023, 1:47pm UTC](https://forum.infinityfree.com/t/unable-to-connect-to-mysql-with-php-pdo/84225/16 "2023-10-18T13:47:29Z")

</div>

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