0

Following is the code:

class Db{
    protected static $connection;
    public function connect(){
        if (!isset(self::$connection)) {
            $config = parse_ini_file('../config.ini');
            self::$connection = new mysqli('localhost',$config['username'],$config['password'],$config['dbname']);
        }
        if (self::$connection === false) {
            die("Error in database connection. Please contact network administrator.");
            return false;
        }
        return self::$connection;
    }
    public function quote($value){
        $connection = $this -> connect();
        return "'".$connection -> real_escape_string(string $value)."'";
    }
}

I am getting the following error in the quote function: Parse error: syntax error, unexpected '$value' (T_VARIABLE) in D:\xampp\htdocs\fizzy\submit.php on line 48

Shreesh Katyayan
  • 101
  • 3
  • 14
  • Is `$value` an array? Why are you trying to cast it as a `string` -> `string $value`? – Sean Nov 02 '15 at 07:19
  • without casting, I get the following warning: Warning: mysqli::real_escape_string() expects parameter 1 to be string, array given in D:\xampp\htdocs\fizzy\submit.php on line 48 – Shreesh Katyayan Nov 02 '15 at 07:38
  • Sorry, error was in function calls and not the function definition. Mark closed :) Thanks for help anyways – Shreesh Katyayan Nov 02 '15 at 08:03

4 Answers4

0

Might be there is no value in $value. So you can do like this:

public function quote($value = ""){

OR

public function quote($value){
        if(isset($value) && $value != "") {
           $connection = $this -> connect();
           return "'".$connection -> real_escape_string(string ($value))."'";
       } else {
           return false;
       }
    }
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
0

Change string $value to (string) $value

tliokos
  • 3,606
  • 3
  • 28
  • 28
0

remove string

public function quote($value){
    $connection = $this -> connect();
    return "'".$connection -> real_escape_string($value)."'";
                                                 ^
}
Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
0

Take a look at last code line

return "'".$connection -> real_escape_string(string $value)."'";

Here parameter passed to function is (string $value).As you are type casting $value,it should be ((string)$value).

Dinesh Belkare
  • 639
  • 8
  • 24