What is the equivalent of bind_result on PDO

后端 未结 4 1949
无人共我
无人共我 2020-12-07 02:06

I\'m converting to PDO and Im using prepared statements, I want to bind my result as so $stmt->bind_result($email_count); so i am able to put this into an if

4条回答
  •  粉色の甜心
    2020-12-07 02:23

    For people who come here seeking a literal answer.

    Use bindColumn

    PDOStatement::bindColumn — Bind a column to a PHP variable

    using the example from the same source :

    function readData($dbh) {
      $sql = 'SELECT name, colour, calories FROM fruit';
      try {
        $stmt = $dbh->prepare($sql);
        $stmt->execute();
    
        /* Bind by column number */
        $stmt->bindColumn(1, $name);
        $stmt->bindColumn(2, $colour);
    
        /* Bind by column name */
        $stmt->bindColumn('calories', $cals);
    
        while ($row = $stmt->fetch(PDO::FETCH_BOUND)) {
          $data = $name . "\t" . $colour . "\t" . $cals . "\n";
          print $data;
        }
      }
      catch (PDOException $e) {
        print $e->getMessage();
      }
    }
    readData($dbh);
    

提交回复
热议问题