2016-02-05 92 views
2

嗨,大家好我有3個表存儲我的數據,我想從他們那裏獲取數據,但是我的書面代碼不工作,請有人告訴我我該怎麼做?如何從php和mysql中的多個表中獲取值

我的第一張表是「權限」,表示權限名稱和id包含在這裏。

CREATE TABLE IF NOT EXISTS `permission` (
`permission_id` int(11) NOT NULL, 
`permission_description` varchar(50) NOT NULL 
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=latin1; 

第二個表是包含帶角色id的角色名稱的「角色」。

CREATE TABLE IF NOT EXISTS `role` (
`id` int(11) NOT NULL, 
`role_group` int(11) NOT NULL, 
`role_name` varchar(50) NOT NULL, 
`role_description` varchar(50) NOT NULL 
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=latin1; 

和第三個「role_permission」包括「permission」表中的哪些權限可以訪問「role」表中的什麼角色。

CREATE TABLE IF NOT EXISTS `role_permission` (
`id` int(11) NOT NULL, 
`role_id` int(11) NOT NULL, 
`permission_id` int(11) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

PHP:

$result3 = $dp->sql_query("select p.*, r.*, a.* from role_permission p, role r, permission a WHERE p.role_id = r.id AND p.permission_id = a.permission_id");  
$rules = array(); 
while($rows3 = $dp->sql_fetchrow($result3)) 
     { 
      $rules[] = array("title" => $rows3['permission_description'], // get value from "permission" table 
          "pages" => $row3['role_name'] . ", ",   // get value from "role" table 
          "pdes" => $row3['role_description']   // get value from "role" table 
        ); 
     } 
$smarty->assign("rules", $rules); 

HTML:

<div class="modal-body"> 
      {foreach $rules as $rule} 
       <blockquote> 
        <p> 
         <strong>{$rule.title}:</strong> 
         <br> 
         <small>{$rule.pages}</small> 
        </p> 
       </blockquote> 
      {/foreach} 
      </div> 
+2

那麼問題是什麼?沒有數據或錯誤的數據? – jarlh

+1

http://dev.mysql.com/doc/refman/5.7/en/join.html – jeroen

+0

我也加了我的html cod,錯誤的是我的html頁面沒有結果 – maxdom

回答

0

顯然在表rolerole_permission同一列id。數組$ rows3將包含錯誤的相同屬性id。 V.

相關問題