[英]Prepared Statement in PHP MVC
我正在嘗試在MVC架構中創建一個簡單的論壇。
這是我的數據庫設置 (相關部分):
表格:forum_categories
`forum_categories` (
`cat_id` INT(8) NOT NULL AUTO_INCREMENT,
`cat_title` VARCHAR(255) NOT NULL,
`cat_desc` TEXT NOT NULL,
PRIMARY KEY (`cat_id`),
UNIQUE KEY (`cat_title`)
表格:forum_topics
`forum_topics` (
`topic_id` INT(8) NOT NULL AUTO_INCREMENT,
`cat_id` INT(8) NOT NULL COMMENT 'foreign key with forum_categories table',
`user_id` INT(11) NOT NULL COMMENT 'foreign key with users table',
`topic_title` VARCHAR(255) NOT NULL,
`topic_desc` TEXT NOT NULL,
`topic_date` DATETIME DEFAULT NULL,
PRIMARY KEY (`topic_id`),
FOREIGN KEY (`cat_id`) REFERENCES forum_categories (`cat_id`) ON DELETE CASCADE ON UPDATE CASCADE
我想實現的功能示例 :
類別1的cat_id = 1
類別2的cat_id = 2
主題1的cat_id = 1
主題2的cat_id = 2
現在,當選擇類別1時,我只希望顯示主題1。
如果選擇category2,我只希望顯示主題2。
此准備的SQL語句實現了:
PREPARE stmnt FROM
'SELECT *
FROM forum_categories fc
JOIN forum_topics ft ON fc.cat_id = ft.cat_id
WHERE fc.cat_id = ?
ORDER BY ft.topic_date DESC';
SET @a = 1;
EXECUTE stmnt USING @a;
我的問題 :我想將此功能移到我的PHP MVC結構中。
這是我的嘗試,不起作用(它顯示所有類別中的所有主題)。
控制者
/**
* Show all the topics in the chosen category
*/
public function showForumTopics()
{
$topic_model = $this->loadModel('Forum');
$this->view->forum_topics = $topic_model->getForumTopics();
$this->view->render('forum/viewTopics');
}
模型
/**
* Gets an array that contains all the forum topics in the database.
* Each array element is an object, containing a specific topic's data.
* @return array All the forum topics
*/
public function getForumTopics($cat_id)
{
$sql = 'SELECT * FROM forum_categories fc JOIN forum_topics ft ON fc.cat_id = ft.cat_id WHERE fc.cat_id = :cat_id ORDER BY ft.topic_date DESC';
$query = $this->db->prepare($sql);
$query->execute(array(':cat_id' => $cat_id));
return $query->fetchAll();
}
視圖
if ($this->forum_topics) {
foreach($this->forum_topics as $key => $value) {
echo '<p><strong>Title:</strong>' . $value->topic_title . '</p>';
echo '<p><strong>Description:</strong> ' . $value->topic_desc . '</p>';
echo '<p><strong>Author:</strong> ' . $value->topic_author . '</p>';
echo '<p><strong>Date:</strong> ' . $value->topic_date . '</p>';
}
} else {
echo 'No forum topics.';
}
幫助將不勝感激! 謝謝!!
例如,您的頁面http://example.com/?cat_id=2您的代碼應像這樣
控制者
public function showForumTopics()
{
$default_category = 1;
$topic_model = $this->loadModel('Forum');
$cat_id = isset($_GET['cat_id']) ? $_GET['cat_id'] : $default_category;
$this->view->forum_topics = $topic_model->getForumTopics($cat_id);
$this->view->render('forum/viewTopics');
}
模型
public function getForumTopics($cat_id) {
$sql = 'SELECT * FROM forum_categories fc JOIN forum_topics ft ON fc.cat_id = ft.cat_id WHERE fc.cat_id = :cat_id ORDER BY ft.topic_date DESC';
$query = $this->db->prepare($sql);
$query->execute(array(':cat_id' => $cat_id));
return $query->fetchAll(); }
視圖
if ($this->forum_topics) {
foreach($this->forum_topics as $key => $value) {
echo '<p><strong>Title:</strong>' . $value->topic_title . '</p>';
echo '<p><strong>Description:</strong> ' . $value->topic_desc . '</p>';
echo '<p><strong>Author:</strong> ' . $value->topic_author . '</p>';
echo '<p><strong>Date:</strong> ' . $value->topic_date . '</p>';
}
} else {
echo 'No forum topics.';
}
您的問題是您的后端需要ID來拉特定的類別(並通過聯接獲得正確的主題)。 在您的數據庫查詢中,您在這里尋找它: WHERE fc.cat_id = ?
您的getForumTopics($cat_id)
函數還需要將ID傳遞到該准備好的語句中。 問題是您在調用該函數時沒有將任何ID傳遞給該函數:
$this->view->forum_topics = $topic_model->getForumTopics();
因此,沒有任何結果,您的函數現在已損壞,應該引發錯誤。 此時,您有兩個選擇:
如果未選擇任何特定類別,請進行備份以列出所有主題。 您可以在函數定義中執行以下操作:
// Specifying the backup option null here, for when there is no ID passed public function getForumTopics($cat_id = null) { // Simple check for an ID if ($id) { // Run the code you had before $sql = 'SELECT * FROM forum_categories fc JOIN forum_topics ft ON fc.cat_id = ft.cat_id WHERE fc.cat_id = :cat_id ORDER BY ft.topic_date DESC'; $query = $this->db->prepare($sql); $query->execute(array(':cat_id' => $cat_id)); return $query->fetchAll(); } } else { // Otherwise, do the same thing as above but without a WHERE clause } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.