-1

我想从我的数据库中获取我的位置citycountry数据location

这是我的代码:

$data = mysql_fetch_array(mysql_query("
SELECT 
u.`prename`,
u.`surname`,
DATE_FORMAT(u.`birthday`, '%d. %M') AS bday,
l.`city`,
l.`country`

FROM 
`users` AS u
LEFT JOIN `users_location` AS ul ON ul.`uid` = u.`id`
LEFT JOIN `locations` AS l ON l.`id` = ul.`location_id`

WHERE u.`id`='".(int) ($_GET['id'])."'"));

Inusers_location分配了哪个用户有哪个location_id

users_location结构:id, uid, location_id

其中locationscitycountry名称。

但是citycountry使用我的查询是空的。我猜是因为我的加入声明?

4

1 回答 1

2

我从未使用过“LEFT JOIN tableAS t.

只需“LEFT JOIN tablet”,就像这样;

SELECT 
    u.`prename`, u.`surname`, DATE_FORMAT(u.`birthday`, '%d. %M') AS bday, 
    l.`city`, l.`country`
FROM  `users` u
LEFT JOIN  `users_location` ul ON  ul.`uid` = u.`id`
LEFT JOIN  `locations` l  ON  l.`id` = ul.`location_id`
WHERE u.`id` = {$id};

编辑:请考虑检查数据,而不仅仅是做你所做的事情。

大意的东西;

$id = (array_key_exists('id', $_GET) && is_string($_GET['id']) && ctype_digit($_GET['id']))
    ? $_GET['id'] : FALSE :
if ($id)
{
    SELECT 
        u.`prename`, u.`surname`, DATE_FORMAT(u.`birthday`, '%d. %M') AS bday, 
        l.`city`, l.`country`
    FROM  `users` u
    LEFT JOIN  `users_location` ul ON  ul.`uid` = u.`id`
    LEFT JOIN  `locations` l  ON  l.`id` = ul.`location_id`
    WHERE u.`id` = {$id};
}
else
{
    echo 'No valid ID was provided.';
}
于 2013-10-25T23:14:52.117 回答