1

How to escape column' special characters in SQL select statement.

I have that sql select statement and the column account_name has values that contains special characters, it gives me error as it doesn't escape those special characters.

select * from account where account_name ='$account_name'  
Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

2

Don't use any of the MySQL extension commands, they are obsolete and not recommended anymore.

use PDO and prepared atatements.

http://php.net/pdo

$name = '$somethingHere';
$stmt = $db->prepare("Select * from account where account_name = :name");
$stmt->bindValue(':name', $name, PDO::PARAM_STR);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
Andrew
  • 12,617
  • 1
  • 34
  • 48
1

Use mysql_real_escape_string($account_name) function.

Bud Damyanov
  • 30,171
  • 6
  • 44
  • 52