stack
stack

Reputation: 10228

How to escape multiple quotes in the query?

I have a query like this:

" SELECT IF(table.edited > 0, '<i title="c.edited"></i>','') AS edited, ... "
^                             ^         ^

The above query has a pdo-syntax-error ...! It is about quote. How can I fix it?

Upvotes: 0

Views: 492

Answers (3)

stack
stack

Reputation: 10228

Try this:

" SELECT IF(table.edited > 0, CONCAT('<i title=\"', c.edited, '\"></i>','') AS edited, ..."

Using CONCAT for combining both c.edited and a string.

Using \ for escaping quote (as @Aaron Mason mentioned in his answer)

Upvotes: -1

Dan
Dan

Reputation: 3815

use PDO bindValue to insert clean string:

$query = "SELECT filename
           FROM Posters
           WHERE name = :name";
$statement = $this->db->prepare($query);
$statement->bindValue(':name', $name);
$statement->execute();
return $statement->fetchAll(PDO::FETCH_ASSOC);

Upvotes: 1

Aaron Mason
Aaron Mason

Reputation: 362

Backslashes, same as any other string in PHP with quotes in it:

" SELECT IF(table.edited > 0, '<i title=\"c.edited\"></i>','') AS edited, ... "

Upvotes: 1

Related Questions