RSS Amplifier

Mydnic.be · Jun 16, 2026

PDO, SQL, LIKE et requêtes préparées

0
Sign in to vote or save

mydnic.be · Mydnic.be

Web Development · Dec 21, 2012

CR

Clément Rigo · @mydnic

IT Business Artisan & Consultant CTO

Si vous aussi vous codez votre site en PHP et que vous utilisez PDO pour gérer la connexion à votre base de donnée, vous aurez peut-être parfois du mal à vous y retrouver, tant le nombre d'exemple sur le net regorge de scripts utilisant mysql... Mon dernier casse-tête en date étant l'utilisation du paramètre LIKE dans une de mes requêtes utilisées pour construire mon formulaire de recherche interne.

En mysql, il est assez facile d'utiliser le LIKE avec vos variables, en voici un example:

$mysql_query = mysql_query("SELECT * FROM blog_articles WHERE article_title LIKE '%{$query}%'");
```

Maintenant, en PDO, avec les requêtes préparées, il va falloir aller dans les détails. Je vous donne une requête fonctionnelle en mille, allez-y faites vous plaisir:

```php
$req = $bdd->prepare('SELECT * FROM blog_articles WHERE article_title LIKE :query');

$req->execute(array(':query' => '%' . $query . '%'));
```

Vous voyez donc maintenant comment bien écrire votre requête avec un LIKE :) Mettez ensuite les <code>%</code> là où vous le souhaitez.

À + !

Myd

CR

Clément Rigo

IT Business Artisan & Consultant CTO. I've been writing this blog since 2010.

Personal

The Fastest Path to a $50K/Month DTC Brand

Joe Speiser built Petflow, a subscription pet food brand, into a $70M business. He later shared the exact playbook he'd follow if he had to start over and get to $50K/month as fast as possible. Here's that playbook, distilled. The plan comes down...

Web Development

mini-swe-agent: The 100 Line AI Agent That Solves GitHub Issues

I was scrolling through my GitHub stars looking for something to write about and stopped on this one again. mini-swe-agent is the kind of project I like: it takes something everyone assumes needs a huge framework (an AI coding agent) and does it i...

Personal · Web Development

How I Built An Automatic Highlight Clipper For Battlefield 6 (Python, OpenCV, FFmpeg)

I record every Battlefield 6 session with NVIDIA's ShadowPlay, because who doesn't want to relive that one clutch double kill at 2am? The problem is that &quot;recording everything&quot; very quickly turns into a graveyard of 4K DVR files nobody wil...

Read the original on mydnic.be

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.