简体   繁体   English

SQL中的while循环只循环一次

[英]While-loop in SQL only loops once

I have a rest service where I send a get request to a table in my database.我有一个休息服务,我向我的数据库中的一个表发送一个 get 请求。 I want to build an array with the response.我想用响应构建一个数组。 I get the array-structure I want with this code but the problem is that it only loops once.我用这段代码得到了我想要的数组结构,但问题是它只循环一次。 Why is this?为什么是这样? If I change the second $result to $result2 it returns false instead of the encoded array.如果我将第二个 $result 更改为 $result2,它将返回 false 而不是编码数组。

/**
 * @param int $id
 * @url periodicalitem
 * @return string
 */
public function getPeriodicalItem($id){

    $mysqli = $this->db-> getConnection();

    $query  = 'SELECT * FROM periodicalitem WHERE
        periodical_id  = ' . $id;

    $result = $mysqli->query($query);
    $arr = array();

    while ($row = $result->fetch_assoc()) {

        $query = 'SELECT * FROM inst_codes WHERE id = ' . $row['inst_code'] . '';
        $result = $mysqli->query($query);


        while ($row2 = $result->fetch_assoc()) {

            if($row['inst_code'] == $row2['id'] ){
                $arr[$row2['id']] = array('name' => $row2['name'],
                                          'data' => $arr[$row2['id']]['data'] ? array_push($arr[$row2['id']]['data'], $row) : array($row) );

            }
        }

    } 

    return json_encode($arr); 

}

You are over-writing $result = $mysqli->query($query);您正在重写$result = $mysqli->query($query); inside the loop.循环内。 Use another variable使用另一个变量

public function getPeriodicalItem($id){

    $mysqli = $this->db-> getConnection();

    $query  = 'SELECT * FROM periodicalitem WHERE
        periodical_id  = ' . $id;

    $result = $mysqli->query($query);
    $arr = array();

    while ($row = $result->fetch_assoc()) {

        $query = 'SELECT * FROM inst_codes WHERE id = ' . $row['inst_code'] . '';
        $result1 = $mysqli->query($query);


        while ($row2 = $result1->fetch_assoc()) {

            if($row['inst_code'] == $row2['id'] ){
                $arr[$row2['id']] = array('name' => $row2['name'],
                                          'data' => $arr[$row2['id']]['data'] ? array_push($arr[$row2['id']]['data'], $row) : array($row) );

            }
        }

    } 

    return json_encode($arr); 

}

The problem was that each iteration created a new array instead of appending the already existing one.问题是每次迭代都会创建一个新数组,而不是附加已经存在的数组。 Here is my working code.这是我的工作代码。

/**
 * @param int $id
 * @url periodicalitem
 * @return string
 */
public function getPeriodicalItem($id){

    $mysqli = $this->db-> getConnection();

    $query  = 'SELECT * FROM periodicalitem WHERE
        periodical_id  = ' . $id;

    $result = $mysqli->query($query);

    //$arr = array();
    while ($row = $result->fetch_assoc()) {
        $row = array_map("utf8_encode", $row);


        $query = 'SELECT * FROM inst_codes WHERE id = ' . $row['inst_code'] . '';
        $result2 = $mysqli->query($query);

        while ($row2 = $result2->fetch_assoc()) {
        $row2 = array_map("utf8_encode", $row2);

                   $current = array(
                                     'id'=>$row['id'], 
                                     'volume'=>$row['volume'],
                                     'code' =>$row['code'],
                                     'archive' => $row['archive']
                                  );


             if(!isset($arr[$row2['id']])){
                $arr[$row2['id']] = array();
                $arr[$row2['id']][] = array('name' => $row2['name'],
                                            'prefix' => $row2['prefix'],
                                            'show' => 'true');
            }


                if(isset($arr[$row2['id']]['data'])){ 
                    $arr[$row2['id']]['data'][] = $current;
                }else{
                    $arr[$row2['id']]['data'] = array($current); 
                }    
        }


    } 
    //$arr['2']['data'][] = array($current); 

    return json_encode($arr); 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM