简体   繁体   English

PHP脚本不返回第一个数组

[英]php script doesn't return the first array

this PHP script is supposed to return the top five losers, the top five gainers and the five most active stocks in the form of arrays called result. PHP脚本应该以结果数组的形式返回前五名的输家,前五名的输家和五支最活跃的股票。 when i run the query in the database it returns the right values but when i run the PHP script in the browser it doesn't return the first array, it starts from the second array ,the second array and so on. 当我在数据库中运行查询时,它返回正确的值,但是当我在浏览器中运行PHP脚本时,它没有返回第一个数组,而是从第二个数组,第二个数组开始,依此类推。 this is the script: 这是脚本:

    switch ($which){
        case 'top':
            $sql = "SELECT * FROM market_table ORDER BY price_change DESC LIMIT 5";
            $result = $this -> connection->query($sql);
            if($result->num_rows > 0){
                $r = mysqli_query($this->connection,$sql);
                $res = mysqli_fetch_array($r);
                $result = array(); 
                while($res = mysqli_fetch_array($r)){
                    array_push($result,array("sympol"=>$res['sympol'],"o_price"=>$res['o_price'],
                    'e_name'=>$res['e_name'], 'price_change'=>$res['price_change'],'perc_change'=>$res['perc_change'],
                    'c_price'=>$res['c_price'],'volume'=>$res['volume'],'h_price'=>$res['h_price'],
                    'l_price'=>$res['l_price'],'deals_n'=>$res['deals_n'],
                    'value'=>$res['value']));
                }
                echo json_encode(array("result"=>$result));
            }           
            else{
                echo "top five: 0 results";
            }
        break;
        case 'bottom':
            $sql = "SELECT * FROM market_table ORDER BY price_change ASC LIMIT 5";
            $result = $this -> connection->query($sql);
            if($result->num_rows > 0){
                $r = mysqli_query($this->connection,$sql);
                $res = mysqli_fetch_array($r);
                $result = array(); 
                while($res = mysqli_fetch_array($r)){
                    array_push($result,array("sympol"=>$res['sympol'],"o_price"=>$res['o_price'],
                    'e_name'=>$res['e_name'], 'price_change'=>$res['price_change'],'perc_change'=>$res['perc_change'],
                    'c_price'=>$res['c_price'],'volume'=>$res['volume'],'h_price'=>$res['h_price'],'l_price'=>$res['l_price'],
                    'deals_n'=>$res['deals_n'],
                    'value'=>$res['value']));
                }
                echo json_encode(array("result"=>$result));
            }           
            else{
                echo "bottom five: 0 results";
            }
        break;
        case'most':
            $sql = "SELECT * FROM market_table ORDER BY deals_n DESC LIMIT 5";
            $result = $this -> connection->query($sql);
            if($result->num_rows > 0){
                $r = mysqli_query($this->connection,$sql);
                $res = mysqli_fetch_array($r);

                $result = array(); 
                while($res = mysqli_fetch_array($r)){
                    array_push($result,array("sympol"=>$res['sympol'],"o_price"=>$res['o_price'],
                    'e_name'=>$res['e_name'], 'price_change'=>$res['price_change'],'perc_change'=>$res['perc_change'],
                    'c_price'=>$res['c_price'],'volume'=>$res['volume'],'h_price'=>$res['h_price'],'l_price'=>$res['l_price'],
                    'deals_n'=>$res['deals_n'],
                    'value'=>$res['value']));
                }
                echo json_encode(array("result"=>$result));
            }           
            else{
                echo "most active five: 0 results";
            }
        break;
    }
$res = mysqli_fetch_array($r);
$result = array();

Why do you have those? 你为什么有那些? You're not saving the result($res) since you're initializing the result($result) to an empty array on the 2nd line. 由于将结果($ result)初始化为第二行的空数组,因此您没有保存结果($ res)。 So that's where your first line disappears. 这就是第一行消失的地方。

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

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