user3346088
user3346088

Reputation: 109

Call to undefined method mysqli::mysqli_escape_string()

I try to insert array into my database. but before that I gt an error :

Call to undefined method mysqli::mysqli_escape_string()

my code as follow:

if(!empty($img_src)){

    $img_src = $db->mysqli_escape_string(serialize($img_src));

    $stmt2 = $db->prepare("INSERT INTO photo_upload(`post_id`,`img_src`) VALUES (?,?)");
    $stmt2->bind_param('is', mysqli_insert_id($db),$img_src);
    $stmt2->execute();
}

Upvotes: 1

Views: 6377

Answers (2)

Phil
Phil

Reputation: 164796

You don't need to escape $img_src if you're using it with bind_param. You'll also have to save mysqli_insert_id($db) into a variable to use it in bind_param as all arguments after the first are passed by reference. Or simply use $db->insert_id...

if(!empty($img_src)){

    $img_src = serialize($img_src);

    $stmt2 = $db->prepare("INSERT INTO photo_upload(`post_id`,`img_src`) VALUES (?,?)");
    if (!$stmt2) {
        throw new Exception($db->error, $db->errno);
    }
    $stmt2->bind_param('is', $db->insert_id, $img_src);
    if (!$stmt2->execute()) {
        throw new Exception($stmt2->error, $stmt2->errno);
    }
}

Upvotes: 0

Rikesh
Rikesh

Reputation: 26431

Should be real_escape_string,

$img_src = serialize($img_src);
$img_src = $db->real_escape_string($img_src);

Reference.

Upvotes: 3

Related Questions