按页面内容的id和侧栏菜单的类别ID检索数据库内容

时间:2015-02-22 12:31:39

标签: php mysql

我的网站上的所有网页都是通过id =?完全没问题。但是,如何从侧栏菜单的相同类别ID中一起检索表格内容?

名为areport的主表。辅助表称为categoriesyears。像这样:

areport table
id | category_id | title | message | post_date | and more..

categories table
id | name

我按ID编辑重新审核内容:

if (!isset($_GET["id"])) {
  header("Location: report.php");
  exit;
}  
$id = $_GET["id"];

try {
  $pdo = db_init();
  $sql = "SELECT 
areport.id AS areport_id,
areport.title AS areport_title,
areport.message AS areport_message,
categories.id AS category_id, 
categories.name AS category_name,
years.id AS year_id,
years.name AS year_name
  FROM areport
  INNER JOIN categories ON areport.category_id = categories.id
  INNER JOIN years ON areport.year_id = years.id
  WHERE areport.id=?";
  $stmt = $pdo->prepare($sql);
  $stmt->execute(array($id));
  $areport = $stmt->fetch();

  $title = $areport["areport_title"];
  $message  = $areport["areport_message"];
  $catid = $areport["category_id"];
  $catname = $areport["category_name"];
  $yearname = $areport["year_name"];

非常感谢任何帮助。

0 个答案:

没有答案