zkanoca
zkanoca

Reputation: 9918

How to use common parameter for Mysql PDO query

I have tried to make a search query and my query statement is like the following:

<?php
$query= $db->prepare("SELECT
                          p.id, 
                          p.name, 
                          p.surname, 
                          p.phone, 
                          t.title, 
                          j.job, 
                          d.department
                       FROM
                          People p 
                       JOIN
                          Titles t ON p.title_id = t.id
                       JOIN 
                          Departments d ON p.dept_id = d.id
                       JOIN 
                          Jobs j ON p.job_id = j.id
                       WHERE
                          p.name LIKE :v1 OR
                          p.surname LIKE :v2 OR 
                          p.phone LIKE :v3 OR 
                       ORDER BY 
                          d.department, 
                          p.name, 
                          p.surname"
              );

$query->bindValue(":v1", $value, PDO::PARAM_STR);
$query->bindValue(":v2", $value, PDO::PARAM_STR);
$query->bindValue(":v3", $value, PDO::PARAM_STR);
?>

$value comes from a textbox in which search string is typed. The query works fine.

What I want to learn is why I have to use 3 parameters for just one value. Why cannot I type the statement like:

p.name LIKE :v1 OR
p.surname LIKE :v1 OR 
p.phone LIKE :v1 OR 

and then bind the parameter once only?

$query->bindValue(":v1", $value, PDO::PARAM_STR);

Upvotes: 1

Views: 71

Answers (2)

fehnomenal
fehnomenal

Reputation: 509

The doc of PDO::prepare() says:

You cannot use a named parameter marker of the same name twice in a prepared statement.

Upvotes: 1

Mihai
Mihai

Reputation: 26784

http://paul-m-jones.com/archives/243 Something about stability in memory handling..

I wrote this as an answer because I cant comment yet.

Upvotes: 4

Related Questions