samedi 25 juin 2016

Use like with PDO on JSON-String


Actualy I know how to use LIKE statement with PDO

Now I have a DB-Table called e.g. foobar

foobar contains a JSON-String like:

{"firstname":"foo","email":"aaa@aaa.aa","lastname":"bar"}

Now before I call my INSERT on the table I want to check if the Email is already in use.

$pdo->query("SELECT * FROM myTable WHERE foobar LIKE ?", array('%' . $email . '%'));

as you can see, I pass the email into the pdo query. Notice ->query is my custom function that handles some stuff. In this case its not important to know what happens.

The problem I have is:

If the entry like above exists then its not possible anymore to add an email thats like:

aa@aaa.aa
^^....only two A's

So I thought I can simply change

array('%' . $email . '%')

to

array('%"' . $email . '"%')

but this doesnt work. Is there a way I can check the whole string part ?

{"firstname":"foo","email":"aaa@aaa.aa","lastname":"bar"}


Aucun commentaire:

Enregistrer un commentaire