Search code examples
phpmysqldatabasesyntaxprepare

MySQL prepare statement using variable


I am using below PHP + MySQL prepare statement to select values from database, passing the variable into the statement using function, however I could not get my wanted result. The problem is I don't know how to using the variable in the prepare statement.

Question: Could you take a look whether the syntax I am using is correct?

public function getToken($committeeValue){
    $stmt = $this->conn->prepare("SELECT u.token FROM users u INNER JOIN committee c ON u.email = c.email WHERE c.'$committeeValue' = 1");
    $stmt->execute();
}

Solution

  • Please try the below one.   
    
     public function getToken($committeeValue){
            $stmt = $this->conn->prepare("SELECT u.token FROM users u INNER JOIN committee c ON u.email = c.email WHERE c.".$committeeValue." = 1");
            $stmt->execute();
        }
    

    I think you are made a mistake to appending a php variable within the string.Please try this.