mysql - php how do you retrieve post from database? -


i know question has been asked many times, can't ever make work code, please bear me, i'm pretty new php oop pdo.

my objective

retrieve post title stored in database using php oop pdo

the problem

it returns error:

undefined property: pdostatement::$fetch 

i looked @ php docs pdostatement

i learned crud this tutorial, can't translate code,

here code:

crud.php

<?php  include 'connection.php';  class crud{      private $db;       function __construct($pdo)     {         $this->db = $pdo;     }      public function create_post($post_title, $post_content)     {         try{             $query = "insert posts(post_title, post_content) values (:ptitle, :pbody)";             $stmt = $this->db->prepare($query);             $stmt->bindparam(':ptitle', $post_title);             $stmt->bindparam(':pbody', $post_content);             $stmt->execute();              return $stmt;         }          catch(pdoexception $e)         {             $e->getmessage();         }      }      public function setquery($sql)     {         try{             $query = $this->db->prepare($sql);             $query->execute();              return $query;          }          catch (pdoexception $e)         {             $e->getmessage();         }     }   } 

index.php (where want posts post)

<?php error_reporting(e_all); require_once 'inc/crud.php';  ?>  <!doctype html> <html lang="en"> <head>     <meta charset="utf-8">     <title>welcome house</title> </head> <body>     <h1>this home page</h1>    <?php  $crud = new crud($pdo);  $sql =  "select * posts";  if($result=$crud->setquery($sql) ) {     if($count = $result->fetch)     {         echo '<p>', $count, '</p>';          while($row = $result->fetch_assoc())         {             echo $row['post_title'], '<br>';         }     } }  ?>  </body> </html> 

i'm pretty confused. recommendations , best practices (for php5 , up)?

fetch function, not property

use instead:

    if($count = $result->fetch())                               ^^ 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -