PDO: bindParam, bindValue never works. Why?

Question

I am new to PDO. I've followed some tutorials and and answers in stackoverflow. But I do not know why my code never works.

public function LoadContents($id){
    $Q = "SELECT * FROM ANDROID WHERE ID = ? ";
       try{
        $stmt = $this->conn->query($Q);
        $stmt->bindParam(1, $id);
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

           $this->HEADLINE = $row['HEADLINE'];
       }
       catch (PDOException $ex){echo $ex->getMessage();}
       }

I tried this too:

public function LoadContents($id){
    $Q = "SELECT * FROM ANDROID WHERE ID = ? ";
       try{
        $stmt = $this->conn->query($Q);
        $stmt->bindValue(1, $id, PDO:PARAM_INT);
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

           $this->HEADLINE = $row['HEADLINE'];
       }
       catch (PDOException $ex){echo $ex->getMessage();}
       }

But I wonder this always works:

public function LoadContents($id){
    $Q = "SELECT * FROM ANDROID WHERE ID = $id ";
       try{
        $stmt = $this->conn->query($Q);
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

           $this->HEADLINE = $row['HEADLINE'];
       }
       catch (PDOException $ex){echo $ex->getMessage();}
       }

How can I properly use bindParam or bindValue in my functions?


Show source
| pdo   | php   | mysql   2017-01-07 15:01 0 Answers

Answers ( 0 )

◀ Go back