【问题标题】:How to get values from multiple tables in php and mysql如何从php和mysql中的多个表中获取值
【发布时间】:2016-02-05 14:26:03
【问题描述】:

大家好,我有 3 个表来存储我的数据,我想从中获取数据,但是我的书面代码无法正常工作,请有人告诉我该怎么做?

我的第一个表是“权限”,其中包含权限名称和 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>

【问题讨论】:

  • 那么问题出在哪里?没有数据,还是数据错误?
  • 我也添加了我的html cod,错误的是我的html页面上没有结果
  • 如果直接对 db 运行 SELECT 会得到正确的结果吗?
  • 在运行 MySQL 查询浏览器或其他什么时,SELECT 是否返回任何数据?

标签: php sql


【解决方案1】:

rolerole_permission 表中的 id 显然是同一列。数组 $rows3 将包含错误的相同属性 id。 五、

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-10
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 2020-11-14
    • 1970-01-01
    相关资源
    最近更新 更多