donglong1465 2015-08-26 10:09
浏览 65
已采纳

foreq循环中的Mysqli_query

I'm having trouble with a mysqli_query from inside a foreach loop, I'm getting a string from a table first, then separating that into an array. Then I try looping through the array and calling a query inside the loop.

$langs_result = mysqli_query($con, "SELECT Languages FROM users WHERE Username = '$username'");

    $row = mysqli_fetch_assoc($langs_result);

    $langs = $row['Languages'];
    $userLangs = str_replace(" ","",$langs);
    $userLangs = explode(",",$langs);
    print_r($userLangs);
    $posts = array();

    foreach($userLangs as $lang){
        echo "$lang <br>";
        $sql = "SELECT * FROM posts WHERE Language = '$lang'";
        $getLangPosts = mysqli_query($con, $sql);
        array_push($posts, mysqli_fetch_assoc($getLangPosts));
    }

    print_r($posts);

for this user the langusges are German, Italian, Danish, and English, but the $posts array only contains the first post found from the first language (German), can anybody help? I am trying to get all of the posts for each language in the $userLangs array.

It's going through the foreach loop okay as the $lang variable that's echoed changes each time but the query still isn't working properly.

Thanks for the help!

  • 写回答

5条回答 默认 最新

  • douping5015 2015-08-26 10:15
    关注

    UPDATE
    See this code:

    <?php
    
    $langs_result = mysqli_query($con, "SELECT Languages FROM users WHERE Username = '$username'");
    
    $row = mysqli_fetch_assoc($langs_result);
    
    $langs = $row['Languages'];
    
    // $langs = 'German, Italian, Danish, French'; added this to run the test
    
    // $userLangs = str_replace(" ","",$langs); this is not needed, see the explode below
    $userLangs = explode(", ",$langs);
    
    foreach($userLangs as $lang){
        echo $lang;
        $sql = "SELECT * FROM posts WHERE Language = '$lang'";
    
        $getLangPosts = mysqli_query($con, $sql); // this is the result of the select *
        while($post = mysqli_fetch_assoc($getLangPosts)){ // iterate over all the results
            $postField = $post["yourChoiceField..say..Title"]; // get something from each row
            array_push($posts, $title); // push into array
        }
    }
    
    print_r($posts);
    

    Since the initial select is based on username I don't believe the first loop is needed so your code was on the right track.
    A second loop was needed to iterate over the posts though and a field to populate the $posts array.


    You need to perform mysqli_fetch_assoc in a loop

    $langs_result = mysqli_query($con, "SELECT Languages FROM users WHERE Username = '$username'");
    
    while($row = mysqli_fetch_assoc($langs_result)){
    
        $langs = $row['Languages'];
    
        $userLangs = str_replace(" ","",$langs); // i don't get why you are doing this though
        $userLangs = explode(",",$langs);
        print_r($userLangs);
    
        $posts = array();
    
        foreach($userLangs as $lang){
            echo "$lang <br>";
            $sql = "SELECT * FROM posts WHERE Language = '$lang'";
            $getLangPosts = mysqli_query($con, $sql);
            array_push($posts, mysqli_fetch_assoc($getLangPosts));
        }
    
        print_r($posts);
    }
    

    It would help to know how what the select query actually returns.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果