How should I escape characters inside this LIKE query?

后端 未结 3 867
北恋
北恋 2021-01-13 03:25

I have a field in one of my tables that contains this string:

!\"#¤%&/()=?´`?=)(/&%¤#\"!\\\'\\\'\"\'

(Only for test purposes ofcour

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-13 03:53

    From MySQL Manual:

    Because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\\n”. To search for “\”, specify it as “\\\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against.

    So, you should escape string for LIKE operator in two steps.

    In PHP it can be like this:

    // Your search string, for example, from POST field
    $string = $_POST['column'];
    
    // First step - LIKE escaping
    $string = str_replace(array('\\', '_', '%'), array('\\\\', '\\_', '\\%'), $string);
    
    // Second step - literal escaping
    $string = mysql_real_escape_string($string);
    
    // Result query
    mysql_query("SELECT * FROM `table` WHERE `column` LIKE '%".$string."%'");
    

    UPDATE:

    MySQL extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

    Use MySQLi

    // Connect to database
    $mysqli = new mysqli('localhost', 'username', 'password', 'database');
    
    // Your search string, for example, from POST field
    $string = $_POST['column'];
    
    // First step - LIKE escaping
    $string = str_replace(['\\', '_', '%'], ['\\\\', '\\_', '\\%'], $string);
    
    // Second step - literal escaping
    $string = $mysqli->real_escape_string($string);
    
    // Result query
    $mysqli->query("SELECT * FROM `table` WHERE `column` LIKE '%{$string}%'");
    

    Use PDO

    // Connect to database
    $conn = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');
    
    // Your search string, for example, from POST field
    $string = $_POST['column'];
    
    // First step - LIKE escaping
    $string = str_replace(['\\', '_', '%'], ['\\\\', '\\_', '\\%'], $string);
    
    // Second step - literal escaping
    $string = $conn->quote($string);
    
    // Result query
    $conn->query("SELECT * FROM `table` WHERE `column` LIKE '%{$string}%'");
    

    Or you can use PDO prepared statement, instead of second step (literal escaping):

    // Connect to database
    $conn = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');
    
    // Your search string, for example, from POST field
    $string = $_POST['column'];
    
    // First step - LIKE escaping
    $string = str_replace(['\\', '_', '%'], ['\\\\', '\\_', '\\%'], $string);
    
    // Prepare a statement for execution
    $statement = $conn->prepare("SELECT * FROM `table` WHERE `column` LIKE ?");
    
    // Execute a prepared statement
    $statement->execute(["%{$string}%"]);
    

提交回复
热议问题