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

3
  • Is $value an array? Why are you trying to cast it as a string -> string $value? Commented Nov 2, 2015 at 7: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 Commented Nov 2, 2015 at 7:38
  • Sorry, error was in function calls and not the function definition. Mark closed :) Thanks for help anyways Commented Nov 2, 2015 at 8:03

4 Answers 4

0

Change string $value to (string) $value

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

1 Comment

Tried. Raises notice for conversion from array to string.
0

remove string

public function quote($value){
    $connection = $this -> connect();
    return "'".$connection -> real_escape_string($value)."'";
                                                 ^
}

4 Comments

Warning: mysqli::real_escape_string() expects parameter 1 to be string, array given in D:\xampp\htdocs\fizzy\submit.php on line 48
print_r($value); see what is there inside that. I think according to error, its array.
Yeah it gives an array
@ShreeshKatyayan How can u convert array to string. It will not work. Use implode()
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).

Comments

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;
       }
    }

1 Comment

$value takes input from form.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.