How to output data when using $stmt->fetch(PDO::FETCH_ASSOC);

后端 未结 1 912
南旧
南旧 2021-02-11 11:03

How do you correctly output data when using $stmt->fetch(PDO::FETCH_ASSOC)? Right now I\'m getting single words as my result when doing

\"select          


        
相关标签:
1条回答
  • 2021-02-11 11:17
     $pdo = new PDO(/*Your credentials*/);
     $sql = `select name, author, title from d_books`
     $stmt = $pdo->prepare($sql);
     $stmt->execute(); 
    
     while ($arr = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo $arr['name'];
     }
    

    or

     $pdo = new PDO(/*Your credentials*/);
     $sql = `select name, author, title from d_books`
     $stmt = $pdo->prepare($sql);
     $stmt->execute(); 
     $arr = $stmt->fetchAll(PDO::FETCH_ASSOC);
     foreach ($arr as $titleData) {
        echo $titleData['name'];
     }
    

    You can always use pdo->query() method when running a query without any parameters, which is faster, but I do not believe that this happens much(running queries without any parameters).

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