简体   繁体   中英

PHP PDO always give only one result

I wrote a SQL query with PDO. DB table has 3 results with match the query. But the PDO shows only one result.

my code is this

conn.php

function connect() {
    $servername = "localhost";
    $dbname = "guiding_db";
    $username = "user";
    $password = "pass";

    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $conn;
}

_admin.php

include_once './conn.php';

function getStudentsRequests(){
    $sql = "SELECT * FROM `user` WHERE signas = 'student' AND accept='0'";


   $result = connect()->query($sql);
   $out = $result->fetch(PDO::FETCH_ASSOC);
   print_r($out);
   return $out;

}

getStudentsRequests();

PDOStatement::fetch() loads a single row only. Use PDOStatement::fetchAll() to load all rows (or use a while loop):

$out = $result->fetchAll(PDO::FETCH_ASSOC);

fetch method return only one row from query. If you want all you need to use while loop or fetchAll method

fetch method return next result (one). If you wish get all results - use methods like fetchAll

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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