多表查询并将查询结果合并为一个多维数组

表cms_top:

  id                   name

  1      国际新闻

  2      国内新闻

表cms_category:

  id      name      tid(连接上级)

  1      国际娱乐新闻   1

  2      国际体育新闻   1

  3      国际时政新闻   1

  4      国内娱乐新闻   2

  5      国内体育新闻   2

表cms_article:

  id    title    ptime    source    click    content    uid(连接cms_user中的ID)    cid(连接上级)    tuijian

 

要将查询内容合并为如下形式的多维数组,之后直接从该多维数组中遍历取得数据:

 

方法:

/*文件 sql_config.inc.php*/
<?php
    //数据库地址
    define('DB_HOST', '');
    //数据库的用户名
    define('DB_USER', '');
    //数据库密码
    define('DB_PASS', '');
    //数据库的字符集
    define('DB_CHARSET', 'utf8');
    //数据库的名称
    define('DB_NAME', '');
    //数据表的前缀
    define('DB_PREFIX', 'cms_');
?>

/*文件 connect_sql.func.php*/
<?php
error_reporting(0);
function connect_sql($sql){
    //1、连接数据库服务器
    //mysql_pconnect持久连接,不推荐使用
    @$link=mysql_connect(DB_HOST,DB_USER,DB_PASS);
    
    //2、判断是否连接成功
    if (!$link){
        echo "数据库服务器连接失败,错误信息为:".mysql_error().",错误号为:".mysql_errno();
        return false;
    }
    
    //3、设置客户端字符集
    //mysql_query("set names utf8");
    mysql_set_charset(DB_CHARSET);
    
    //4、选择数据库
    mysql_select_db(DB_NAME,$link);
    
    //5、准备sql语句
      
    //6、发送sql语句
    $res=mysql_query($sql);
    
    //7、处理结果
    if (is_resource($res)){
        //若$sql为查询语句则$res应当返回为结果集资源,需要解析
        while ($row=mysql_fetch_assoc($res)){
            $data[]=$row;
        }
        mysql_free_result($res);
        mysql_close();
        return $data;//注意:这里返回的$data为二维数组
    }else{
        //若$sql为增、删、改则判断其受影响行数
        if ($res){
            $rows=mysql_affected_rows();
            mysql_close();
            return $rows;
        }else{
            echo mysql_error();
            mysql_close();
            return false;
        }
    }
}
?>

<?php
include '../config/sql_config.inc.php';
include '../common/connect_sql.func.php';

//将顶级分类、二级分类和文章表关联组成多维数组
$sql01="select id,name from ".DB_PREFIX."top";
$tops=connect_sql($sql01);
foreach ($tops as &$val01){  //加&,遍历数组键值的同时添加'cates'=>array()元素
    $sql02="select id,name from ".DB_PREFIX."category where tid=".$val01['id'];
    $cates=connect_sql($sql02);
    $val01['cates']=$cates;
    
    foreach ($val01['cates'] as &$val02){
        $sql03="select id,title,ptime from ".DB_PREFIX."article where cid=".$val02['id'];
        $arts=connect_sql($sql03);
        $val02['arts']=$arts;
    }
}

var_dump($tops);

?>

结果为:

array(2) {
  [0]=>
  array(3) {
    ["id"]=>
    string(1) "1"
    ["name"]=>
    string(12) "国际新闻"
    ["cates"]=>
    array(3) {
      [0]=>
      array(3) {
        ["id"]=>
        string(1) "1"
        ["name"]=>
        string(18) "国际娱乐新闻"
        ["arts"]=>
        array(1) {
          [0]=>
          array(3) {
            ["id"]=>
            string(1) "1"
            ["title"]=>
            string(54) "超甜!英国小哥霉霉演唱会上向女友求婚"
            ["ptime"]=>
            string(10) "1530235016"
          }
        }
      }
      [1]=>
      array(3) {
        ["id"]=>
        string(1) "2"
        ["name"]=>
        string(18) "国际体育新闻"
        ["arts"]=>
        array(2) {
          [0]=>
          array(3) {
            ["id"]=>
            string(1) "2"
            ["title"]=>
            string(53) "英格兰丢单刀获第2 比利时全胜将战日本"
            ["ptime"]=>
            string(10) "1530235177"
          }
          [1]=>
          array(3) {
            ["id"]=>
            string(1) "6"
            ["title"]=>
            string(51) "2018年世界杯,荷兰、意大利未进入32强"
            ["ptime"]=>
            string(10) "1530261159"
          }
        }
      }
      [2]=>
      array(3) {
        ["id"]=>
        string(1) "3"
        ["name"]=>
        string(18) "国际时政新闻"
        ["arts"]=>
        array(1) {
          [0]=>
          array(3) {
            ["id"]=>
            string(1) "3"
            ["title"]=>
            string(65) "环球时报社评:美要世界不买伊朗石油 中国怎么办"
            ["ptime"]=>
            string(10) "1530235270"
          }
        }
      }
    }
  }
  [1]=>
  &array(3) {
    ["id"]=>
    string(1) "2"
    ["name"]=>
    string(12) "国内新闻"
    ["cates"]=>
    array(2) {
      [0]=>
      array(3) {
        ["id"]=>
        string(1) "4"
        ["name"]=>
        string(18) "国内娱乐新闻"
        ["arts"]=>
        array(2) {
          [0]=>
          array(3) {
            ["id"]=>
            string(1) "4"
            ["title"]=>
            string(36) "陈奕迅赴俄罗斯观看世界杯"
            ["ptime"]=>
            string(10) "1530235474"
          }
          [1]=>
          array(3) {
            ["id"]=>
            string(1) "7"
            ["title"]=>
            string(77) "倪妮baby牵手荡秋千画面文艺唯美 两人灿笑开心的像个孩子"
            ["ptime"]=>
            string(10) "1530414358"
          }
        }
      }
      [1]=>
      &array(3) {
        ["id"]=>
        string(1) "5"
        ["name"]=>
        string(18) "国内体育新闻"
        ["arts"]=>
        array(1) {
          [0]=>
          array(3) {
            ["id"]=>
            string(1) "5"
            ["title"]=>
            string(63) "姚明在场下目睹这一幕:中国内线竟被韩国打爆"
            ["ptime"]=>
            string(10) "1530235548"
          }
        }
      }
    }
  }
}

 

posted @ 2018-07-01 17:58  Autumn_n  阅读(1179)  评论(0编辑  收藏  举报
TOP