Multiple table queries and merging query results into one multidimensional array

Source: Internet
Author: User

Table Cms_top:

  ID name

1 International News

2 Domestic News

Table Cms_category:

  ID name tid (connect to Superior)            

1 International Entertainment News 1

2 International Sports News 1

3 International political News 1

4 Domestic Entertainment News 2

5 Domestic Sports News 2

Table Cms_article:

  ID title ptime Source Click Content UID (ID in connection cms_user) CID (connection ancestor) Tuijian        

To merge the contents of the query into a multidimensional array of the following form, the data is then traversed directly from the multidimensional array:

Method:

/*file sql_config.inc.php*/<?PHP//Database Address    Define(' Db_host ', '); //user name of the database    Define(' Db_user ', '); //Database Password    Define(' Db_pass ', '); //the character set of the database    Define(' Db_charset ', ' UTF8 '); //name of the database    Define(' Db_name ', '); //prefix of data table    Define(' Db_prefix ', ' Cms_ ');?>/*file connect_sql.func.php*/<?PHPerror_reporting(0);functionConnect_sql ($sql){    //1, Connection database server//mysql_pconnect persistent connection, not recommended to use@$link=mysql_connect(Db_host,db_user,Db_pass); //2. Determine if the connection is successful    if(!$link){        Echo"The database server connection failed with an error message:".Mysql_error(). ", Error Number:".Mysql_errno(); return false; }        //3, set the client character set//mysql_query ("Set names UTF8");Mysql_set_charset (Db_charset); //4. Select Database    mysql_select_db(Db_name,$link); //5. Prepare SQL statement//6, send SQL statement    $res=mysql_query($sql); //7. Processing Results    if(Is_resource($res)){        //if $sql is a query statement then $res should be returned as a result set resource, need to parse         while($row=Mysql_fetch_assoc($res)){            $data[]=$row; }        Mysql_free_result($res); Mysql_close(); return $data;//Note: The $data returned here is a two-dimensional array}Else{        //if $sql to increase, delete, Gerze determine the number of affected rows        if($res){            $rows=mysql_affected_rows(); Mysql_close(); return $rows; }Else{            Echo Mysql_error(); Mysql_close(); return false; }    }}? ><?PHPinclude‘.. /config/sql_config.inc.php ';include‘.. /common/connect_sql.func.php ';//make a multidimensional array of top-level, two-level, and article-table associations$sql= "Select Id,name from". Db_prefix. " Top;$tops=connect_sql ($sql);foreach($tops  as&$val){//add & To iterate over the array key value while adding the ' Cates ' =>array () element    $sql= "Select Id,name from". Db_prefix. " Category where Tid= ".$val[' ID ']; $cates=connect_sql ($sql); $val[' Cates ']=$cates; foreach($val[' Cates '] as&$val){        $sql= "Select Id,title,ptime from". Db_prefix. " Article where cid= ".$val[' ID ']; $arts=connect_sql ($sql); $val[' Arts ']=$arts; }}Var_dump($tops);?>

The result is:

Array(2) {  [0]=>Array(3) {    ["id"]=>string(1) "1"    ["Name"]=>string(12) "International News"    ["Cates"]=>Array(3) {      [0]=>Array(3) {        ["id"]=>string(1) "1"        ["Name"]=>string(18) "International Entertainment News"        ["Arts"]=>Array(1) {          [0]=>Array(3) {            ["id"]=>string(1) "1"            ["Title"]=>string(54) "Super sweet!" To propose to his girlfriend at the British little Brother Mold concert.            ["Ptime"]=>string(10) "1530235016"          }        }      }      [1]=>Array(3) {        ["id"]=>string(1) "2"        ["Name"]=>string(18) "International Sports News"        ["Arts"]=>Array(2) {          [0]=>Array(3) {            ["id"]=>string(1) "2"            ["Title"]=>string(53) "England lost a single pole won the 2nd Belgian victory will fight Japan"            ["Ptime"]=>string(10) "1530235177"          }          [1]=>Array(3) {            ["id"]=>string(1) "6"            ["Title"]=>string(51) "2018 World Cup, the Netherlands, Italy did not enter the top 32"            ["Ptime"]=>string(10) "1530261159"          }        }      }      [2]=>Array(3) {        ["id"]=>string(1) "3"        ["Name"]=>string(18) "International Political News"        ["Arts"]=>Array(1) {          [0]=>Array(3) {            ["id"]=>string(1) "3"            ["Title"]=>string(65) "Global Times Editorial: What does the United States want the world not to buy Iranian oil china?"            ["Ptime"]=>string(10) "1530235270"          }        }      }    }  }  [1]=> &Array(3) {    ["id"]=>string(1) "2"    ["Name"]=>string(12) "Domestic News"    ["Cates"]=>Array(2) {      [0]=>Array(3) {        ["id"]=>string(1) "4"        ["Name"]=>string(18) "Domestic Entertainment News"        ["Arts"]=>Array(2) {          [0]=>Array(3) {            ["id"]=>string(1) "4"            ["Title"]=>string(36) "Eason Chan to Russia to watch the World Cup"            ["Ptime"]=>string(10) "1530235474"          }          [1]=>Array(3) {            ["id"]=>string(1) "7"            ["Title"]=>string(77) "Ivanini Baby holding hand swing screen art aesthetic Two people laugh happy like a child"            ["Ptime"]=>string(10) "1530414358"          }        }      }      [1]=> &Array(3) {        ["id"]=>string(1) "5"        ["Name"]=>string(18) "Domestic Sports News"        ["Arts"]=>Array(1) {          [0]=>Array(3) {            ["id"]=>string(1) "5"            ["Title"]=>string(63) "Yao Ming on the field witnessed this scene: China's interior has been hit by South Korea"            ["Ptime"]=>string(10) "1530235548"          }        }      }    }  }}

Multiple table queries and merging query results into one multidimensional array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.