PHP PDO search for a value in two or more columns using one string

前端 未结 4 1129
清酒与你
清酒与你 2020-12-10 23:54

When I want to find a value from a row using PDO I use the following method:

//Search whether user exists
$sqlQueryEmailLogin = $dbh->prepare(\"SELECT ven         


        
相关标签:
4条回答
  • 2020-12-11 00:06

    That's what I'm using

    $sql = "SELECT * FROM articles WHERE id = :id AND status = :status"; 
    $stmt = $conn->prepare($sql);
    $stmt->execute(array(':id' => $id , ':status' => $status));
    
    0 讨论(0)
  • 2020-12-11 00:13

    You should use OR instead of AND. That way, you will get all rows that match either by id or by status.

    SELECT * FROM articles WHERE id = ? OR status = ?
    
    0 讨论(0)
  • 2020-12-11 00:25

    You could simplify it by using the method described by @gbestard. But you should also do this:

    $search = 'asdf'; // fill this with your form input
    
    $sql = "SELECT * FROM articles WHERE id = :id OR status = :status"; 
    $stmt = $conn->prepare($sql);
    $stmt->execute(array(
        ':id'     => $search, 
        ':status' => $search,
    ));
    

    Notice the change to OR in the query, and supplying the $search multiple times...

    0 讨论(0)
  • 2020-12-11 00:28

    Try the following

    $sql = "SELECT * FROM articles WHERE id = :id AND status = :status";
    $stmt = $conn->prepare($sql);
    $stmt->bindValue(':id', $id); 
    $stmt->bindValue(':status', $status); 
    $stmt->execute();
    

    See docs http://php.net/manual/en/pdostatement.bindvalue.php

    0 讨论(0)
提交回复
热议问题