دستورات پر کاربرد PDO
نمونه کد کلاس:
class comment { private $_db; function __construct() { $servername = "localhost"; $username = "root"; $password = "mysql"; $db = "wnews"; try { $this->_db = $con = new PDO("mysql:host=$servername;dbname=$db", $username, $password); $this->_db->exec('set names utf8'); // set the PDO error mode to exception return $con; } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage(); } } function showComments() { $q = $this->_db->prepare('select id, post_id, content,auther,date,email from comments'); $q->execute(); return $q; } }
برای خواندن آیتم های خروجی:
$comments = new comment(); $selComments= $comments->showComments(); while($val = $selComments->fetch(PDO::FETCH_ASSOC)) { echo 'email:' . $val['email'] . '<br>'; }