PHP Query无法正确返回数据

时间:2012-01-12 21:31:16

标签: php wordpress

我有一个从多个表中提取值的查询。我想通过shows_date ASC订购它们,我似乎无法输出任何数据,任何人都能看到我的语法问题吗?

$artists = $wpdb->get_results("SELECT * FROM " . GIGPRESS_ARTISTS . " AS a, " . GIGPRESS_SHOWS . " AS s, ORDER BY s.show_date ");

var_dump($artists);

foreach($artists as $artist_group) 
{
    $shows = $wpdb->get_results("SELECT * 
                                 FROM " . GIGPRESS_ARTISTS . " AS a, " . GIGPRESS_VENUES . " as v, " . GIGPRESS_SHOWS ." AS s 
                                 LEFT JOIN  " . GIGPRESS_TOURS . " AS t 
                                   ON s.show_tour_id = t.tour_id 
                                 WHERE " . $date_condition . " 
                                   AND show_status != 'deleted' AND s.show_artist_id = " . $artist_group->artist_id . " 
                                   AND s.show_artist_id = a.artist_id AND s.show_venue_id = v.venue_id " . $further_where . " 
                                 ORDER BY s.show_date " . $sort . ",s.show_expire " . $sort . ",s.show_time ". $sort . $limit);
}

DUMP RETURNS

array(72){[0] => object(stdClass)#260(25){[“artist_id”] => string(1)“1”[“artist_name”] => string(14)“Damien Dempsey”[“artist_order”] => string(1)“0”[“show_id”] => string(1)“1”[“show_artist_id”] => string(1)“1”[“show_venue_id”] => string(1)“1”[“show_tour_id”] => string(1)“0”[“show_date”] => string(10)“2012-01-29”[“show_multi”] => string(1)“0”[“show_time”] => string(8)“20:30:00”[“show_expire”] => string(10)“2012-01-29”[“show_price”] => string(7)“£10.00”[“show_tix_url”] => string(0)“”[“show_tix_phone”] => string(0)“”[“show_ages”] => string(8)“All Ages”[“show_notes”] => string(0)“”[“show_related”] => string(1)“0”[“show_status”] => string(7)“deleted”[“show_tour_restore”] => string(1)“0”[“show_address”] => NULL [“show_locale”] => NULL [“show_country”] => NULL [“show_venue”] => NULL [“show_venue_url”] => NULL [“show_venue_phone”] => NULL} [1] => object(stdClass)#259(25){[“artist_id”] => string(1)“2”[“artist_name”] => string(10)“Gary Dunne”[“artist_order”] => string(1)“0”[“show_id”] => string(1)“1”[“show_artist_id”] => string(1)“1”[“show_venue_id”] => string(1)“1”[“show_tour_id”] => string(1)“0”[“show_date”] => string(10)“2012-01-29”[“show_multi”] => string(1)“0”[“show_time”] => string(8)“20:30:00”[“show_expire”] => string(10)“2012-01-29”[“show_price”] => string(7)“£10.00”[“show_tix_url”] => string(0)“”[“show_tix_phone”] => string(0)“”[“show_ages”] => string(8)“All Ages”[“show_notes”] => string(0)“”[“show_related”] => string(1)“0”[“show_status”] => string(7)“deleted”[“show_tour_restore”] => string(1)“0”[“show_address”] => NULL [“show_locale”] => NULL [“show_country”] => NULL [“show_venue”] => NULL [“show_venue_url”] => NULL [“show_venue_phone”] => NULL} [2] => object(stdClass)#261(25){[“artist_id”] => string(1)“3”[“artist_name”] => string(19)“London Irish Center”[“artist_order”] => string(1)“0”[“show_id”] => string(1)“1”[“show_artist_id”] => string(1)“1”[“show_venue_id”] => string(1)“1”[“show_tour_id”] => string(1)“0”[“show_date”] => string(10)“2012-01-29”[“show_multi”] => string(1)“0”[“show_time”] => string(8)“20:30:00”[“show_expire”] => string(10)“2012-01-29”[“show_price”] => string(7)“£10.00”[“show_tix_url”] => string(0)“”[“show_tix_phone”] => string(0)“”[“show_ages”] => string(8)“All Ages”


我尝过以下

foreach($artists as $artist_group) {
        $shows = $wpdb->get_results("SELECT * FROM " . GIGPRESS_ARTISTS . " AS a, " . GIGPRESS_VENUES . " as v, " . GIGPRESS_SHOWS ." AS s LEFT JOIN  " . GIGPRESS_TOURS . " AS t ON s.show_tour_id = t.tour_id WHERE " . $date_condition . " AND show_status != 'deleted' ORDER BY s.show_date ASC " . $limit);

哪种方法适用于排序,但它输出的每个值约为100次或者是愚蠢的......

2 个答案:

答案 0 :(得分:0)

AS之后的逗号是无关的

  

SELECT * FROM“.GIGPRESS_ARTISTS。”AS a,“。GIGPRESS_SHOWS。   “AS s,ORDER BY s.show_date

另外,您似乎缺少WHERE子句或JOIN ON语句。

这个查询将产生与(所有演出)x(所有乐队)的交叉连接,这样在你的foreach中你将为系统中的每个演出处理一次,而不是每个乐队/节目一次。

答案 1 :(得分:0)

嗯,你肯定有一个结果,但是它返回一个对象数组,每个对象都是一个返回的行,而不是你可能期望的多维数组。

要获取对象中的数据,您应该可以执行以下操作:

//initlialise array
$array = array();

//loop through array of objects
foreach($artists as $row){
    $temp_array = array();
    $temp_array['artist_id'] = $row->artist_id;
    $temp_array['artist_name'] = $row->artist_name;
    ....
    //add rest of object data to temporary array
    ....
    //assign data to main array
    $array[] = $temp_array;
}

然后,这将从对象获取数据并创建数据的多维数组。您可能希望在那里处理您的数据。这只是一个例子,因为我不知道你希望如何使用数据。

P.S。正如zerkms所说,如果您愿意,可以使用$array[4]->artist_name访问数据,但我误解了您的问题。如果您确实需要将其存储为多维数组,则可以使用上面的代码。

如果您尚未修复JOIN条件,则需要将其添加到第一个SQL语句中,如果没有它,您将收到笛卡尔连接,这将为您提供重复行,因为您没有提供连接条件。这将解决100x行相同的问题。在ORDER之前的最后一个条件中也不需要逗号,因为这会导致SQL语法错误。