/ Published in: PHP
Function to prevent sql injection in Like queries, where the characters '_' and '%' can be dangerous.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php function escapeLike($mysql, $data) { $escaped = $mysql->real_escape_string($data); } //Usage $dangerous_input = '%My Name'; //$mysql has to be either an instance of mysql or mysqli $query = "SELECT * FROM tbl WHERE field LIKE '" . escapeLike($mysql, $dangerous_input) . "%'"; echo $query; //Echoes: SELECT * FROM tbl WHERE field LIKE '\%My Name%' ?>