Php MySQL左连接,创建多维数组后

Php MySQL左连接,创建多维数组后,php,mysql,arrays,multidimensional-array,left-join,Php,Mysql,Arrays,Multidimensional Array,Left Join,我有2个表,外键是'timesection_id'。 第一个表:timesection |id |姓名|时间|位置| 第二个表是内容 |id | title | short | desc | desc | img | position | timesection | id| 我想创建一个到第一个表的左联接。它必须按位置排序。 结果应该如下所示: array (size=1) 'content' => array (size=2) 1 =>

我有2个表,外键是'timesection_id'。 第一个表:timesection


|id |姓名|时间|位置|


第二个表是内容


|id | title | short | desc | desc | img | position | timesection | id|

我想创建一个到第一个表的左联接。它必须按位置排序。 结果应该如下所示:

array (size=1)
  'content' => 
    array (size=2)
      1 => 
        array (size=3)
          'time' => string '12-00' (length=5)
          'name' => string 'Start' (length=21)
          'performer' => 
            array (size=3)
                   0=>
                        'title' => string '12-00'
                        'desc' => string 'etc etc'
                        'shor_desc' => string 'etc etc'
                        'img' => string '1.jpg'
                   1=>
                        'title' => string '12-00'
                        'desc' => string 'etc etc'
                        'shor_desc' => string 'etc etc'
                        'img' => string '1.jpg'
      2 => 
        array (size=3)
          'time' => string '13-00' (length=5)
          'name' => string 'Something' (length=24)
          'performer' => 
            array (size=3)
                   0=>
                        'title' => string '12-00'
                        'desc' => string 'etc etc'
                        'shor_desc' => string 'etc etc'
                        'img' => string '1.jpg'
                   1=>
                        'title' => string '12-00'
                        'desc' => string 'etc etc'
                        'shor_desc' => string 'etc etc'
                        'img' => string '1.jpg'
我的sql:

SELECT * FROM timesection
    LEFT OUTER JOIN content ON  timesection_id = timesection.id
    ORDER BY timesection.position ASC
我有一个结果:

 0 => 
    array (size=9)
      'id' => null
      'name' => string 'KezdĂŠs' (length=7)
      'time' => string '11:00' (length=5)
      'position' => null
      'title' => null
      'short_desc' => null
      'desc' => null
      'img' => null
      'timesection_id' => null
  1 => 
    array (size=9)
      'id' => string '1' (length=1)
      'name' => string 'KezdĂŠs, Sikerdal debĂźtĂĄlĂĄsa' (length=31)
      'time' => string '12:00-14.00' (length=11)
      'position' => string '1' (length=1)
      'title' => string 'Ăv Embere orvoscsoport' (length=23)
      'short_desc' => string '' (length=0)
      'desc' => string 'PĂŠldĂĄtlan ĂśsszefogĂĄs eredmĂŠnyekĂŠnt egĂŠszsĂŠges gyermeket szĂźlt ĂŠs nĂŠgy ĂŠletet mentett meg egy tĂśbb hĂłnapja agyhalott asszony Debrecenben' (length=149)
      'img' => string '1.jpg' (length=5)
      'timesection_id' => string '2' (length=1)
  2 => 
    array (size=9)
      'id' => string '2' (length=1)
      'name' => string 'KezdĂŠs, Sikerdal debĂźtĂĄlĂĄsa' (length=31)
      'time' => string '12:00-14.00' (length=11)
      'position' => string '2' (length=1)
      'title' => string 'Varga RĂłbert' (length=13)
      'short_desc' => string '' (length=0)
      'desc' => string 'Varga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbertVarga RĂłbert' (length=156)
      'img' => string '1.jpg' (length=5)
      'timesection_id' => string '2' (length=1)
  3 => 
    array (size=9)
      'id' => string '3' (length=1)
      'name' => string 'KezdĂŠs, Sikerdal debĂźtĂĄlĂĄsa' (length=31)
      'time' => string '12:00-14.00' (length=11)
      'position' => string '3' (length=1)
      'title' => string 'BenkĹ Vilmos ĂŠs fiatal tehetsĂŠgek' (length=36)
      'short_desc' => string '' (length=0)
      'desc' => string 'BenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgekBenkĹ Vilmos ĂŠs fiatal tehetsĂŠgek' (length=396)
      'img' => string '1.jpg' (length=5)
      'timesection_id' => string '2' (length=1)
如果主表中有来自子表的更多记录,我希望创建一个performer值并将其放入子数组中。性能值可以为空。

SQL查询:

SELECT timesection.id,timesection.name,timesection.time,
   content.title,content.short_desc,content.desc,content.img,content.id as cid
   FROM timesection
   LEFT JOIN content ON content.timesection_id = timesection.id
   ORDER BY timesection.position , content.position

PHP代码:(注意:使用旧的不推荐的MySQL函数*,考虑使用MySQL或PDO代替)


你的期望是什么?SQL查询还是php代码?不同之处在于,您发布的代码看起来像某些框架的sql配置,但完全不清楚哪个是正确的。你能更新一下你的问题吗?SQL或PHP代码对我很好。我编辑了这个问题。谢谢你的评论!我收到以下错误消息:致命错误:在第1行的path\showContent.php中调用未定义的函数fetch_object(),请更正我的错误。我只需要编写mysql\u fetch\u对象,一切都很好。又来了。
$r = mysql_query($query);
$array = array('content'=>array());
$i=0;
$lastid = null;
while($row=mysql_fetch_object($r))
{
 if($lastid!==$row->id)
 {
  $array['content'][++$i]=array('time'=>$row->time,'name'=>$row->name,'performer'=>array());
  $lastid=$row->id;
 }
 if($row->cid!==null)
 {
  $array['content'][$i]['performer'][]=array('title'=>$row->title,'short_desc'=>$row->short_desc,'desc'=>$row->desc,'img'=>$row->img);
 }
}