zemerick
zemerick

Reputation: 1

PDO prepared statements Like and OR statements used together

I'm trying to say if all the 'OR's match then display. If it's remotely close to the custodian then display (LIKE) To Leo, Yes I tried :custodian heres whole code. I commented out what you suggested because that's what works for now. (also changed the sql). I'm curious if the first query where I'm trying to get the 'count' matters. Let me know. Thanks.

$q = $_GET['q'];
$STH = $dbh->prepare("SELECT COUNT(*) FROM inv_assets WHERE po = :query OR serialNum = :query OR dop = :query OR purchaseFrom = :query OR custodian = :query");
    $STH->bindParam(':query', $q);
            //$STH->bindParam(':custodian', '%'.$q.'%');
    $STH->execute();

    if ($STH->fetchColumn() > 0) {
        $STH = NULL;
        $STH = $dbh->prepare("SELECT * FROM inv_assets WHERE po = :query OR serialNum = :query OR dop =:query OR purchaseFrom = :query OR custodian = :query");
        $STH->bindParam(':query', $q);
        //$STH->bindParam(':custodian', '%'.$q.'%');
                    $STH->execute();
        showTable($STH,$perms);

Upvotes: 0

Views: 1436

Answers (3)

Westcoast Rider
Westcoast Rider

Reputation: 1

<?php

$STH = $dbh->prepare("SELECT COUNT(*) FROM inv_assets 
                      WHERE po = :query OR serialNum = :query OR dop = :query 
                      OR purchaseFrom = :query OR custodian LIKE :custodian");

$custodian = "%".$q."%";
$STH->bindParam(':custodian', $custodian);
$STH->bindParam(':query', $q);
$STH->execute();

?>

this will avoid passing parameter 2 by reference error and works

Upvotes: 0

Leo Haris
Leo Haris

Reputation: 561

An example of how to do PDO LIKE Queries.

$STH = $dbh->prepare("SELECT COUNT(*) FROM inv_assets 
                      WHERE po = :query OR serialNum = :query OR dop = :query 
                      OR purchaseFrom = :query OR custodian LIKE :custodian");

$ret = $STH->execute(array(':custodian' => '%'.$query.'%',':query' => $query));

Example with bindParam:

<?php

$STH = $dbh->prepare("SELECT COUNT(*) FROM inv_assets 
                      WHERE po = :query OR serialNum = :query OR dop = :query 
                      OR purchaseFrom = :query OR custodian LIKE :custodian");

$STH->bindParam(':custodian', '%'.$q.'%');
$STH->bindParam(':query', $q);
$STH->execute();

?>

Upvotes: 2

Soufiane Hassou
Soufiane Hassou

Reputation: 17750

LIKE replaces = not OR

Example:

SELECT first_name, last_name 
FROM student_details 
WHERE first_name LIKE 'S%' OR last_name LIKE 'A%';

Take a look here: SQL Comparison Keywords

Upvotes: 2

Related Questions