数组内容不正确

时间:2013-08-26 08:55:34

标签: php sql

我目前有这个SQL语句:

SELECT p.*,k.name FROM posts AS p 
    LEFT JOIN `post-keywords` AS pk ON p.id = pk.id_post 
    LEFT JOIN keywords AS k ON pk.id_keyword = p.id
    ORDER BY p.id DESC LIMIT :num,:count
但是,它没有按预期工作。这是服务器返回的内容:

array(7) {
  [0]=>
  array(5) {
    ["id"]=>
    string(1) "2"
    ["title"]=>
    string(28) "Everything about Internet"
    ["text"]=>
    string(41) "Article itself"
    ["date"]=>
    string(1) "0"
    ["name"]=>
    NULL
  }
  [1]=>
  array(5) {
    ["id"]=>
    string(1) "1"
    ["title"]=>
    string(16) "Internet"
    ["text"]=>
    string(32) "Text text text"
    ["date"]=>
    string(10) "1371186330"
    ["name"]=>
    string(7) "tag1"
  }
  [2]=>
  array(5) {
    ["id"]=>
    string(1) "1"
    ["title"]=>
    string(16) "Internet"
    ["text"]=>
    string(32) "Text text text"
    ["date"]=>
    string(10) "1371186330"
    ["name"]=>
    string(7) "tag2"
  }
  [3]=>
  array(5) {
    ["id"]=>
    string(1) "1"
    ["title"]=>
    string(16) "Internet"
    ["text"]=>
    string(32) "Text text text"
    ["date"]=>
    string(10) "1371186330"
    ["name"]=>
    string(7) "tag3"
  }
  [4]=>
  array(5) {
    ["id"]=>
    string(1) "1"
    ["title"]=>
    string(16) "Internet"
    ["text"]=>
    string(32) "Text text text"
    ["date"]=>
    string(10) "1371186330"
    ["name"]=>
    string(7) "tag4"
  }
  [5]=>
  array(5) {
    ["id"]=>
    string(1) "1"
    ["title"]=>
    string(16) "Internet"
    ["text"]=>
    string(32) "Text text text"
    ["date"]=>
    string(10) "1371186330"
    ["name"]=>
    NULL
  }
  [6]=>
  array(5) {
    ["id"]=>
    string(1) "1"
    ["title"]=>
    string(16) "Internet"
    ["text"]=>
    string(32) "Text text text"
    ["date"]=>
    string(10) "1371186330"
    ["name"]=>
    NULL
  }
}

如何从包含多个name字段的服务器检索数组:

array (5) {
   ["id"] =>
   string (1) "1"
   ["title"] =>
   string (16) "Internet"
   ["text"] =>
   string (32) "text text text"
   ["date"] =>
   string (10) "1371186330"
   ["name"] =>
   string (10) "word1"
   ["name"] =>
   string (10) "word2"
   ["name"] =>
   string (10) "word3"
}

1 个答案:

答案 0 :(得分:1)

目前,您的查询是“覆盖”字段,其中包含多个具有相同名称的条目。例如,您获得了p.namek.name。你的查询没有区别,所以它被覆盖(我推测)k.name

为了确保两者兼顾,请写下以下内容:SELECT p.name AS p_name, k.name AS k_name