Jump to content

PDO-Prepared Statements using mysqli_real_escape_string


Go to solution Solved by Jacques1,

Recommended Posts

 PDO-Prepared Statements using mysqli_real_escape_string

Is it a good Idea to use mysqli_real_escape_string  for extra  security  In the Prepared Statements

<?php

	try {
      
	  require_once '../includes/pdo_connect.php';
      $make = mysqli_real_escape_string($_GET['make']);
      $sql = 'SELECT *
                FROM cars
                WHERE make LIKE :make AND yearmade >= :yearmade AND price <= :price
                ORDER BY price';
        $stmt = $db->prepare($sql);
        $stmt->bindValue(':make', '%' . $make . '%');
        $stmt->bindParam(':yearmade', $_GET['yearmade'], PDO::PARAM_INT);
        $stmt->bindParam(':price', $_GET['price'], PDO::PARAM_INT);
        $stmt->execute();
        $errorInfo = $stmt->errorInfo();
        if (isset($errorInfo[2])) {
            $error = $errorInfo[2];
        }
    } catch (Exception $e) {
        $error = $e->getMessage();
    }

  • Solution

No, it's not a good idea. It's actually plain wrong.

 

What you need to understand is that the parameters of a prepared statement are treated as data. They're not processed by the SQL parser (that's the whole point). So if you have backslashes in your string, then you end up with literal backslashes. And that will certainly break your search function.

 

You either use a prepared statement, or you manually escape the values. But you can't have both.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.