douzhang5121 2019-08-13 05:08
浏览 108

如何在Codeigniter中转义数据库连接详细信息中的字符

I have a database credentials where some escape characters are present in password like \ and '

Below is the actual password: 12r\3u%DMRCy'\q!@W2aq

I want use that password by escaping these characters, however codeigniter shows error

when used password with double quote, single quote and escape strings like below in database.php

'password' => "12r\3u%DMRCy'\q!@W2aq"
'password' => "12r\\3u%DMRCy'\q!@W2aq"
'password' => '12r\\3u%DMRCy\'\\q!@W2aq'

It shows below error.

Message: pg_connect(): Unable to connect to PostgreSQL server: missing "=" after "\q!@W2aq'" in connection info string

However when tried to echo like below it perfectly prints my original password 12r\3u%DMRCy'\q!@W2aq

echo '12r\\3u%DMRCy\'\\q!@W2aq';
echo "12r\\3u%DMRCy'\q!@W2aq";

Below is the sample code of connection in database.php

$db['staging_db'] = array(
    'dsn' => '',
    'hostname' => 'test.rds.amazonaws.com',
    'username' => 'test_username',
    'password' => "12r\\3u%DMRCy'\q!@W2aq",
    'database' => 'staging',
    'dbdriver' => 'postgre',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE,
    'port' => 5432,
);

Can anyone please suggest how can I use my above mentioned password in database.php with codeigniter?

  • 写回答

2条回答 默认 最新

  • doushi1847 2019-08-13 05:46
    关注

    I tried below code

    'password' => addslashes("12r\\3u%DMRCy'\q!@W2aq")

    and it takes my original password 12r\3u%DMRCy'\q!@W2aq and get connected to database. Thanks!

    评论

报告相同问题?