GatesPlan
GatesPlan

Reputation: 497

PDO, can't find whats wrong in my prepared statement

I'm trying to make simple search function but there's an error I cannot find why.

I checked the result SQL query is properly generated. However, execute this prepared statement cause unknown failure. Why this execute() returns false?

public function searchConcept(string $cplx = '', string $name = '', string $desc = '', int $page = 0)
{
    $SQL = 'select * from concept ';
    $SQL_criteria = array();

    // Criteria
    if($cplx) { $SQL_criteria[] = 'cplx:cplx'; }
    if($name) { $SQL_criteria[] = 'name like :name'; }
    if($desc) { $SQL_criteria[] = 'description like :desc'; }
    if($SQL_criteria) { $SQL .= 'where '.implode(' and ', $SQL_criteria).' '; }

    $SQL .= 'limit :page, 15';

    $stmt = $this->db->prepare($SQL);

    if($cplx) { $stmt->bindValue(':cplx', $cplx); }
    if($name) { $stmt->bindValue(':name', '%'.$name.'%'); }
    if($desc) { $stmt->bindValue(':desc', '%'.$desc.'%'); }

    $stmt->bindValue(':page', $page*15);

    if($stmt->execute()) {
        $rst = $stmt->fetchAll(\PDO::FETCH_OBJ);
        return $rst;
    } else {
        throw new DBRFError();
    }
}

DBRFError is custom error class that I made.

#

Add 1. #### I think cplx:cplx doesn't matter. $cplx takes its value something like ">5". I didn't actually tested that certain situation but I just tested without cplx block and also cause an error.

#

Add 2. #### I just find the origin of the problem. $SQL .= 'limit :page, 15'; it cause the error. I do this for pagenation. Without this block, code works well. However, I can't understand why.

Upvotes: 1

Views: 41

Answers (1)

krasipenkov
krasipenkov

Reputation: 2029

You have missing '=' in your where clause. This is the fixed code:

 if($cplx) { $SQL_criteria[] = 'cplx=:cplx'; }

Upvotes: 2

Related Questions