CodeIgniter Forums

Full Version: [BUG&FIX;] Connect to database using dsn with non-default port
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
CI Version: 2.1

My mysql listens on a non-default port for some security reasons. When I tried to connect to my db with DSN, CI just report can't connect. I double checked my DSN and can't find any problem, so I check the code of CI, I found the problem and fixed it. Here is it:

When CI parse the dsn, it forgets the port of DSN, so add it.


file: {CI base}/system/database/DB.php
lines:

Code:
$params = array(
                            'dbdriver'  => $dns['scheme'],
                            'hostname'  => (isset($dns['host'])) ? rawurldecode($dns['host']) : '',
                            'username'  => (isset($dns['user'])) ? rawurldecode($dns['user']) : '',
                            'password'  => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '',
                            'database'  => (isset($dns['path'])) ? rawurldecode(substr($dns['path'], 1)) : ''
                        );

Change to:

Code:
$params = array(
            'dbdriver'  => $dns['scheme'],
            'hostname'  => (isset($dns['host'])) ? rawurldecode($dns['host']) . ( (isset($dns['port'])) ? ':' . rawurldecode($dns['port']) : ''  )  : '',
            'username'  => (isset($dns['user'])) ? rawurldecode($dns['user']) : '',
            'password'  => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '',
            'database'  => (isset($dns['path'])) ? rawurldecode(substr($dns['path'], 1)) : ''
        );