bakrim
bakrim

Reputation: 145

PHP query with string contain quotes

Hi i need to do a request to mysql to get some data where filed name = $letter but the variable $letter may contain ' my problem is this i try to use this :

$letter = mysqli_real_escape_string($conn,strtolower($letter));
$us = $conn->query("SELECT id FROM singers where trim(LOWER(name)) = '".$letter."'");

My problem is in if $letter was I'm legend, after executing this line it become I\'m legend so it can't be find in database because in database it's stored as i'm legend how i can resolve this and get right result.

 echo $letter;
      $letter = mysqli_real_escape_string($conn,strtolower($letter));
        echo $letter;

RESULT

i'm

i\'m

Upvotes: 0

Views: 1230

Answers (2)

bakrim
bakrim

Reputation: 145

enter image description here

enter image description here

there is difference between field and value in where the \ added is the problem

Upvotes: 0

Passionate Coder
Passionate Coder

Reputation: 7294

why you have used

trim and LOWER in your column name

"SELECT id FROM singers where trim(LOWER(name)) = '".$letter."'" //why trim and LOWER

use this

"SELECT id FROM singers where name = '".$letter."'"

Also try this like

$letter = mysqli_real_escape_string($conn,strtolower($letter));
$sql = "SELECT id FROM singers where name = '".$letter."'";
// echo $sql; die; print this and run in  phpmyadmin to check
$us = $conn->query($sql);
if ($us !== false) {
echo 'done';
 }
else{ die('failed!' . mysqli_error($conn));}

Upvotes: 1

Related Questions