How to refer to database connection in codeigniter

2019-06-22 15:45发布

问题:

How can I call manually a PHP database function on CodeIgniter's database handler object? How to retrieve the connection ($dbc), or call a function like mysql_real_escape_string($dbc, $variable)?

回答1:

You can call any mysql native function and access mysql connection id.

See CodeIgniter User Guide



回答2:

It is a better idea to use codeIgiter's functions: $this->db->escape() ,$this->db->escape_str() ,$this->db->escape_like_str()

The function mysql_real_escape_string() is deprecated by now ( see: http://php.net/mysql_real_escape_string ).

Instead use mysqli_real_escape_string or mysqli::real_escape_string (see for the syntax: http://www.php.net/manual/en/mysqli.real-escape-string.php)



回答3:

$this->db->conn_id will get you the current connection link object if you are in a CI context. It will return a mysqli or mysql link object that you can pass into functions like mysql_real_escape_string or the updated mysqli, which actually requires the link object. Source