1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| class Category{
private $host = 'localhost';
private $user = 'root';
private $pass = '';
private $dbname = 'bdd';
private $dbh;
private $error;
private $_categoryid;
private $_categoryname;
private $_postid;
private $_posttitle;
private $_nombrepost;
private $_nombrecategory;
private $stmt;
public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
public function getCategoryid() {
return $this->_categoryid;
}
public function setCategoryid($categoryid) {
$this->_categoryid = $categoryid;
}
public function getCategoryname() {
return $this->_categoryname;
}
public function setCategoryname($categoryname) {
$this->_categoryname = $categoryname;
}
public function getPostid() {
return $this->_postid;
}
public function setPostid($postid) {
$this->_postid = $postid;
}
public function getPosttitle() {
return $this->_posttitle;
}
public function setPosttitle($posttitle) {
$this->_posttitle = $posttitle;
}
public function getNombrepost() {
return $this->_nombrepost;
}
public function setNombrepost($nombrepost) {
$this->_nombrepost = $nombrepost;
}
public function getNombrecategory() {
return $this->_nombrecategory;
}
public function setNombrecategory($nombrecategory) {
$this->_nombrecategory = $nombrecategory;
}
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function rowCount(){
return $this->stmt->rowCount();
}
} // class end
$database = new Category();
$database->query('SELECT * FROM category');
$rows = $database->resultset();
echo "<pre>";
print_r($rows);
echo "</pre>"; |
Partager