无法检索MySQL ID

时间:2012-03-18 19:30:32

标签: php mysql

为什么我无法以这种方式获取ID:

 . $row['ID'] . 

或者这样

 . $row['events.ID'] . 

当我的代码看起来像这样?非常感谢你!

编辑:我没有收到任何错误,但该字段为空白。

$query =    
"SELECT
    events.ID,
    events.EVENT_NAME,
    events.start_datetime,
    events.end_datetime,
    events.VENUE_LOCATION,
    events.PARTY_TYPE,
    events.SMALL_POSTER_URL,
    events.ENTRANCE_PRICE, 
    venues.VENUE_NAME,
    venues.BEER_PRICE,
    venues.WINE_PRICE,
    venues.SPIRITS_PRICE, 
    party_types.PARTYTYPE,
    GROUP_CONCAT(music_styles.MUSIC_STYLE_NAME) AS MUSIC_STYLE_NAME
FROM events
INNER JOIN venues 
    ON events.VENUE_LOCATION = venues.ID
INNER JOIN party_types
    ON events.PARTY_TYPE = party_types.ID
INNER JOIN events_music_styles
    ON events.ID = events_music_styles.event_id
INNER JOIN music_styles
    ON events_music_styles.music_style_id = music_styles.id
WHERE start_datetime >= '$DATE_START_SELECTED'
AND end_datetime < '$DATE_END_SELECTED' 
GROUP BY events.ID
";



$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){

2 个答案:

答案 0 :(得分:1)

我想,你只是错过了mysql_fetch_assoc(..)

这样做:

$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);

然后您可以使用$row["ID"]

如果这不起作用,请尝试

SELECT
 events.ID as id,
 events.EVENT_NAME,
 ...

然后您可以通过$row["id"]

访问它

玩得开心!

答案 1 :(得分:1)

使用mysql_fetch_row($result)会将行作为枚举数组返回,您需要使用mysql_fetch_assoc($result)来获取关联数组