0

i having problem understanding what is wrong with my inheritance. I just can not call most of the function of the parent. below is the function i am stuck.

class MySQLi_DB  extends mysqli {
    private static $_instance = null;
    private function __construct($db="test",$host="localhost", $user="root", $pass="") 
    {

        parent::__construct($host, $user, $pass, $db);
        if (mysqli_connect_error()) {
            die('Connect Error (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
    }
 static public function getDB()
    {
        if(self::$_instance == null)
        {
            self::$_instance = new MySQLi_DB();
        }
        return self::$_instance;

    }


public function insert($table,$data)
        {
           $sql = $this->getQuery($table,$data);
           print $sql;
        }

        public function getQuery($table, $inserts) 
        {
            $lambda = function($value){
                return $this->real_escape_string($value);
            };

            $values = array_map($lambda,$inserts);
            $keys = array_keys($inserts);

            return 'INSERT INTO `'.$table.'` (`'.implode('`,`', $keys).'`) VALUES (\''.implode('\',\'', $values).'\')';
        }

I am having problem calling real_escape_string() function . i have tried using $this->real_escape_string() but i just have to see this error

Fatal error: Using $this when not in object context in D:\wamp\www\Driver\MySQLi_DB.php on line 49

ok may be that could be limitation of lambda function but i have tried it other way by declaring a callback for array_map that also doesn't allow me to call real_escape_string.

i am calling the function as below.

require_once 'MySQLi_DB.php';
        $db = MySQLi_DB::getDB();

        $insert_data = array("cmsId"=>444,"pageName"=>"New Insert");
        $db->insert("cms",$insert_data);

Please point out where i am doing wrong and what could be the best way to do it. thanks

3
  • where is the real_escape_string() function? Commented Jun 7, 2011 at 5:14
  • Can you post line 49 of MySQLi_DB.php if possible? Commented Jun 7, 2011 at 5:17
  • thats is the line in lambda function Commented Jun 7, 2011 at 5:20

2 Answers 2

3
$lambda = function($value){
    return $this->real_escape_string($value);
};

On your 'lambda' function the context is not your object, so $this is not available.

As an alternative, you could use, for example:

$values = array_map('mysql_real_escape_string', $inserts)

or even try:

$values = array_map( array($this, 'real_escape_string'), $inserts)

this is untested but *should* work...

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

2 Comments

you rock maan. it is working amazing...can you please teach me what does this line of your means (array ( $this, 'real_escape_string') ?
A callback (what is passed to array_map) can be either: a function, a function name, an array of (object, function), or array of (static class name, static function name)
2

You can't use the $this keyword into a lamba function as the scope of the function does not extend to the object whose method contains the lambda

Try a different approach:

 public function getQuery($table, $inserts) 
 {
      $values = array_map(array($this, 'real_escape_string'),$inserts);
      $keys = array_keys($inserts);

      return sprintf('INSERT INTO %s (`%s`) VALUES ("%s")',
           $table,
           implode('`,`', $keys),
           implode('","', $values)
      );
    }

Addendum

If real_escape_string is not a method of your object but the standard msqli method you should change the line

 $values = array_map(array($this, 'real_escape_string'),$inserts);

to

 $values = array_map(array(self::$_instance, 'real_escape_string'),$inserts);

to call the method real_escape string.

The array array(self::$_instance, 'real_escape_string') is a callback array and it is used when you want to call an object method.

The php manual states

A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1

and self::$_instance is the mysqli instance you want to call

3 Comments

can you please tell me how else i can use the real_escape_string of mysqli.
@g-molvi: Edited the answer to respond to your comment
Thanks Eineki, that is the real solution

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.