stack stack - 1 year ago 69
SQL Question

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


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

$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
return $conn;


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);
return $out;



Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download