PHP为什么我的INNER JOIN返回错误?

时间:2014-06-05 20:39:31

标签: php mysql inner-join

我有这行代码:

$query = mysqli_query($connection, "SELECT `id`, `image`, `link`, `order` FROM `galleryImages` INNER JOIN `galleries` ON `galleryImages`.`galleryId` = `galleries`.`id`");

它返回此错误:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result

完整代码:

$query = mysqli_query($connection, "SELECT `id`, `image`, `link`, `order` FROM `galleryImages` INNER JOIN `galleries` ON `galleryImages`.`galleryId` = `galleries`.`id`");
                $results = array();
                while($row = mysqli_fetch_assoc($query)){
                        $results[] = $row;
                }
                return $results;

2 个答案:

答案 0 :(得分:1)

尝试:

"SELECT `id`, `image`, `link`, `order`
  FROM `galleryImages` `g`
  INNER JOIN `galleries` `g2` ON `g`.`galleryId` = `g2`.`id`"

答案 1 :(得分:1)

这应该有用..

SELECT 
    `galleries`.`id`, 
    `galleries`.`image`, 
    `galleries`.`link`, 
    `galleries`.`order` 
FROM 
    `galleryImages` 
    INNER JOIN `galleries` 
        ON `galleryImages`.`galleryId` = `galleries`.`id`