douyao4632 2017-12-11 20:11
浏览 62
已采纳

PHP - 从数组中查询多个表

I'm really struggling trying to get these two queries (below) together with INNER JOIN. I'm passing an array through in _GET. I wanna select from two different mysql tables.

URL array _GET example: www.mysite.com/users_slideshows[]=one&users_slideshows[]=two&users_slideshows[]=three

Here are my two queries that i'm trying to combine:

$mysqli = new mysqli('localhost','root','1234','root');
$records = array();

foreach ($_GET["users_slideshows"] as $djahjkdsh) {
    $result = $mysqli->query("SELECT * FROM playlist_builder WHERE playlist_builder.volume IN ('".$djahjkdsh."') ");
    while($row = $result->fetch_array(MYSQL_ASSOC)) {
            $records[] = array('tags' => array($row));
    }
    $json = json_encode($records, JSON_PRETTY_PRINT);

}

foreach ($_GET["users_slideshows"] as $djahjkdsh) {
    $result = $mysqli->query("SELECT * FROM custom_slideshow WHERE custom_slideshow.volume IN ('".$djahjkdsh."') ");
    while($row = $result->fetch_array(MYSQL_ASSOC)) {
            $records[] = array('tags' => array($row));
    }
    $json = json_encode($records, JSON_PRETTY_PRINT);

}

If I just choose from one table, it works perfect but it's only when I combine both together.

I've tried the following below... not happening

SELECT * FROM playlist_builder INNER JOIN custom_slideshow ON custom_slideshow.volume = playlist_builder.volume IN ('".$djahjkdsh."')

If anyone could help me on what I'm doing wrong, I would extremely appreciate it. Thanks!

  • 写回答

1条回答 默认 最新

  • dongwo7858 2017-12-11 21:44
    关注

    Your SQL syntax is incorrect since you run two expressions in same logical ON clause. Simply separate the ON clauses into multiple expressions. Also, with one value, use equality over IN.

    SELECT * FROM playlist_builder 
    INNER JOIN custom_slideshow ON custom_slideshow.volume = playlist_builder.volume 
    AND playlist_builder.volume = ?)
    

    Alternatively, with a WHERE clause:

    SELECT * FROM playlist_builder 
    INNER JOIN custom_slideshow ON custom_slideshow.volume = playlist_builder.volume 
    WHERE playlist_builder.volume = ?)
    

    PHP (using parameterization and table aliases)

    $sql = 'SELECT * FROM playlist_builder p 
            INNER JOIN custom_slideshow c ON c.volume = p.volume AND p.volume = ?';
    
    foreach ($_GET["users_slideshows"] as $djahjkdsh) {
    
        $stmt = $mysqli->prepare($sql);
        $stmt->bind_param("s", $djahjkdsh);
        $stmt->execute();
    
        $result = $stmt->get_result();
        while($row =  $result->fetch_assoc()) {
             $records[] = array('tags' => array($row));
        }
        $json = json_encode($records, JSON_PRETTY_PRINT);    
    
        $stmt->close();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题