CodeIgniter Forums
[BUG&FIX;] Connect to database using dsn with non-default port - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter Archived (from EllisLab) (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: [BUG&FIX;] Connect to database using dsn with non-default port (/thread-52738.html)



[BUG&FIX;] Connect to database using dsn with non-default port - El Forum - 06-24-2012

[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)) : ''
        );