4

I have code igniter installed on server with database I want to run the same db on my mac, I used MAMP and I copy the project folder inside htdocs, but I have this error would you please help me!

ErrorException [ 8192 ]: mysql_escape_string(): This function is deprecated; use mysql_real_escape_string() instead.
5
  • Do you use that function anywhere within your code? What you've posted here isn't relevant. Commented Oct 2, 2014 at 20:53
  • @tadman I update the question, actually I am new on this topic I have to run the project I got this error Commented Oct 2, 2014 at 20:56
  • It looks like it's falling back to some kind of compatibility mode because it can't find mysqli. Which modules does it have loaded? You can usually find out with phpinfo(). Commented Oct 2, 2014 at 20:58
  • @tadman I did not get how to find it Commented Oct 2, 2014 at 21:04
  • as an aside, it may help you to have a config/staging and config/development folder in which you add your customised database.php file. common databases can then be included from the config/database.php file. Commented Dec 13, 2014 at 12:31

6 Answers 6

9

Don't be afraid to change core files, just alter FCPATH/system/database/drivers/mysqli/mysqli_driver.php

function escape_str($str, $like = FALSE)
{
    if (is_array($str))
    {
        foreach ($str as $key => $val)
        {
            $str[$key] = $this->escape_str($val, $like);
        }

        return $str;
    }

    if (function_exists('mysqli_real_escape_string') AND is_object($this->conn_id))
    {
        $str = mysqli_real_escape_string($this->conn_id, $str);
    }
    else
    {
        $str = addslashes($str);
    }

    // escape LIKE condition wildcards
    if ($like === TRUE)
    {
        $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
    }

    return $str;
}

I had the same issue


Better solution -> https://ellislab.com/forums/viewthread/228288/ "stated in github that it will be fixed in CodeIgniter 3.0 the fix already exists in that repository"

Sign up to request clarification or add additional context in comments.

3 Comments

CodeIgniter 2.2.0 was released in June of this year. The GitHub repo has lots of activity. Maybe it's dead to you, but it's not dead.
How do you set the ENVIRONMENT constant (in index.php)?
@Gervs For a dead platform, it's amazing that 3.0 is in beta and will be fully released later this year. FYI, modifying the core files is never the way to go. A.) there's probably a supported update when you encounter such issues (as there is here). B.) it's the whole reason behind the folders in the application directory. You make extended modifications there and incorporate.
6

Try this

function escapeString($val) {
    $db = get_instance()->db->conn_id;
    $val = mysqli_real_escape_string($db, $val);
    return $val;
}

Comments

1

try this:

$db['development']['hostname'] = 'mysql:host=localhost';
$db['development']['dbdriver'] = 'pdo';

$db['staging']['hostname'] = 'mysql:host=localhost';
$db['staging']['dbdriver'] = 'pdo';

I have update the answer

2 Comments

then I have this error, PDOException [ 1049 ]: SQLSTATE[42000] [1049] Unknown database 'bmgr1'
copying the project folder inside htdocs to your machine almost certainly didn't bring the MySQL database with it; you will need to export the database from the production machine, and then import it into your local MySQL instance.
0

also, to use the function mysqli_real_escape_str, you need the mysqli , to get it

function get_mysqli() { 
$db = (array)get_instance()->db;
return mysqli_connect('localhost', $db['username'], $db['password'], $db['database']);}

The above function returns a mysqli object such that you can use,

$escaped = mysqli_real_escape_string(get_mysqli(),$string);

It works well, not so important here though!

Comments

0

It's not goot idea edit core CI files. If you don't want see deprecated warnings from mysql_escape_string, to use mysql_real_escape_string() instead you need open connection with DB. Use db->initialise() in your base controller

class Base_controller extends CI_Controller {
 function __construct() {
     parent::__construct();

    $this->load->database('db_name');
    $this->db->initialize();

Comments

-3

make a function in Model class which will use database connection string and pass the value from controller to convert it with mysqli_real_escape_string()

function MysqliRealeScapeString($val){

    $new = mysqli_real_escape_string($this->db->conn_id, $val);
    return $new;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.