简体   繁体   English

从SQL选择数组

[英]Select from SQL into an array

I tried to find some related answer but didn't find anything to work. 我试图找到一些相关的答案,但没有任何工作。

I want to select * from an table ( currently there are 5 rows in that table ) and the results stored in an array. 我想从一个表中选择*(当前该表中有5行),并将结果存储在数组中。

This is my function. 这是我的职责

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;
                         }



        }

The results that I am getting : 我得到的结果:

{"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}]

MODIFIED CODE : 修改后的代码:

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;
        }
    }

It returns just one row. 它仅返回一行。 The first row of the query. 查询的第一行。

You dont need a prepare statement you can use this code for get all result set. 您不需要准备语句,可以使用此代码获取所有结果集。

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

$ride now is an array associative with all records from rides $ ride现在是与游乐设施中所有记录关联的数组

You are breaking the execution flow of the function in the if the else it is not necessary. 如果没有其他必要,则会破坏函数的执行流程。

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

Thanks to Ramsés Fernández, Final working code: 感谢RamsésFernández,最终工作代码:

public function getRideByDestination($destinatie) { 公共功能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;

} }

可能您可以像$data[$index] = array_map('id', $row['id'])

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

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