从SQL中选择一个数组

时间:2017-11-21 18:42:10

标签: php mysql json

我试图找到一些相关的答案,但没有找到任何可行的方法。

我想从表中选择*(当前该表中有5行),结果存储在数组中。

这是我的功能。

public function listAllRides() {

        $index = 0;
        $row = array();
        $data = array();
        $result = array();

        $resu = $this->conn->prepare("SELECT * FROM rides ");

           $resu->execute();
           $ride = $resu->get_result()->fetch_assoc();

            // this echo is to check if the query works. But only the first row 
                // is printed here  
           echo json_encode($ride)."<br>";


            if ($resu->execute()) {

                while ($row = $resu->fetch()) {
                $data[$index]['id'] = $row;
                $data[$index]['unique_id'] = $row;
                $data[$index]['destinatie'] = $row;

                 // also tried this :
                //$data[$index]['id'] = $row['id'];
                //$data[$index]['unique_id'] = $row['unique_id'];
                //$data[$index]['destinatie'] = $row['destinatie'];

                $index++;

                echo json_encode($data)."<br>";

                    }
                    echo json_encode($data)."<br>";
                    $resu->close();

                }
                else {
                            return false;
                         }



        }

我得到的结果:

{"id":34,"unique_id":"59f8afdc2bf201.64404165","destinatie":"dera","driver":"123","kmride":"112233","created_at":"2017-10-31 19:16:12","updated_at":null}

[{"id":true,"unique_id":true,"destinatie":true}]

[{"id":true,"unique_id":true,"destinatie":true},"id":true,"unique_id":true,"destinatie":true}]

[{"id":true,"unique_id":true,"destinatie":true},"id":true,"unique_id":true,"destinatie":true},{"id":true,"unique_id":true,"destinatie":true}]

[{"id":true,"unique_id":true,"destinatie":true},"id":true,"unique_id":true,"destinatie":true},"id":true,"unique_id":true,"destinatie":true},"id":true,"unique_id":true,"destinatie":true}]

[{"id":true,"unique_id":true,"destinatie":true},"id":true,"unique_id":true,"destinatie":true},"id":true,"unique_id":true,"destinatie":true},"id":true,"unique_id":true,"destinatie":true},{"id":true,"unique_id":true,"destinatie":true}]

修改后的代码:

public function listAllRides() {

        $index = 0;
        $row = array();
        $data = array();
        $result = array();

        $resu = $this->conn->query("SELECT * FROM rides ");

        $ride = $resu->fetch_all(MYSQLI_ASSOC);
        return $ride;
}

It works, like this .. thanks for the answer Ramsés Fernández

What if I need to narrow the select : 

public function getRideByDestination($destinatie) {

        $stmt = $this->conn->prepare("SELECT * FROM rides WHERE destinatie = ?");
         $stmt->bind_param("s", $destinatie);

         if ($stmt->execute()) {
            $ride = $stmt->get_result()->fetch_assoc();
            $stmt->close();

              return $ride;
            }
         else {
            return false;
        }
    }

只返回一行。查询的第一行。

3 个答案:

答案 0 :(得分:0)

您不需要准备语句,您可以使用此代码获取所有结果集。

   $resu = $this->conn->query("SELECT * FROM rides ");
   $ride = $resu->fetch_all(MYSQLI_ASSOC);

$ ride now是一个与来自游乐设施的所有记录相关联的数组

如果没有必要,你打破了函数的执行流程。

if ($stmt->execute()) {
$result = $stmt->get_result();
    while ($row = $result->fetch_assoc())
    {
       $array[]=$row;
    }
  $stmt->close();
  return array;
}
return false;

答案 1 :(得分:0)

感谢RamsésFernández, 最终工作代码:

公共函数getRideByDestination($ destinatie){

    $index = 0;
    $data = array();
        $stmt = $this->conn->prepare("SELECT * FROM rides WHERE destinatie = ?");
            $stmt->bind_param("s", $destinatie);

         if ($stmt->execute()) {
    while ($row = $result->fetch_assoc()){
            $data[] = $row;

            }
    }
        $stmt->close();

        return $data;

}

答案 2 :(得分:-1)

可能你可以像{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } }, "placeto": { "login": "fsdfsdfsfddfdfdfdf", "trankey": "sdfsdfsdfsdfsdf" } }

那样进行分配