2020-04-09 21:28:10 +08:00

94 lines
2.4 KiB
PHP

<?php
namespace MessageBoard\Database;
require_once FOLDER_ROOT.'/lib/Database/Database.php';
class DbMessage extends Database
{
/**
* 取得列表
*
* @param integer $limit 第幾筆開始
* @param integer $count 此查詢要列出幾筆
* @return void
*/
public function getList(int $limit=null, int $count=null)
{
$sqlString = "SELECT * FROM `".$this->table('message')."`";
$query = $this->connDB->prepare($sqlString);
$query->execute();
$queryResultAll = $query->fetchAll();
return $queryResultAll;
}
/**
* 取得單筆資料
*
* @param int $id
* @return array
*/
public function getDataByid($id)
{
$sqlString = "SELECT * FROM `".$this->table('message')."` WHERE id = :id";
$query = $this->connDB->prepare($sqlString);
$query->bindParam(":id", $id);
$query->execute();
$queryResultAll = $query->fetch();
return $queryResultAll;
}
/**
* 建立資料
*
* @param string $title 標題
* @param string $content 內容
* @return int 建立後的ID
*/
public function insert($title, $content)
{
$sqlString = "INSERT INTO `".$this->table('message')."` (title,content) VALUES ( :title, :content )";
$query = $this->connDB->prepare($sqlString);
$query->bindParam(":title", $title);
$query->bindParam(":content", $content);
$query->execute();
return $this->connDB->lastInsertId();
}
/**
* 編輯
*
* @param int $id 編號
* @param string $title 標題
* @param string $content 內容
* @return int 是否有成功
*/
public function edit($id, $title, $content)
{
$sqlString = "UPDATE `".$this->table('message')."` SET content= :content ,title= :title WHERE id= :id";
$query = $this->connDB->prepare($sqlString);
$query->bindParam(":id", $id);
$query->bindParam(":title", $title);
$query->bindParam(":content", $content);
$query->execute();
return $query->rowCount();
}
public function delete($id)
{
$sqlString = "DELETE FROM `".$this->table('message')."` WHERE id= :id";
$query = $this->connDB->prepare($sqlString);
$query->bindParam(":id", $id);
$query->execute();
return $query->rowCount();
}
}