PDO query only returning one result

Hi there guys!

I’m trying to figure out why my PDO query intended to get all rows in a table is only returning the first row.

	/* Retrieve current list of servers, both for server count and ip/port/name check */
	$stmt = $pdo1->prepare("SELECT * FROM servers");


	$serverlist[] = $stmt->fetch(PDO::FETCH_ASSOC);
	if($serverlist == NULL){
		echo 'serverlist is null.';

There’s two rows in the table but when I execute the script, I get only the first row in the table:

    [0] => Array
            [id] => 1
            [name] => Demo
            [safename] => demo
            [ip] => domain.com
            [port] => 30120
            [dbhost] =>
            [dbname] => demo
            [dbuser] => user
            [dbpass] => pass
            [framework] => none


Could someone tell me what I’m doing wrong?

Thanks for your time!

I’ll assume your not interested in a scrollable cursor. You should be able to see what you’re doing wrong by reading under Description and See Also. If not, or you’re unsure, please ask.


And if it’s not clear after reading that, let me highlight a particular word in your original post that I think you might be missing from your code…

Hi there guys!

No offense to @Mittineague, but I wasn’t smart enough to figure out from his response what I was doing wrong so I did a bit more Googling and did end up at a SE post explaining what got all rows. I was shocked to find it was fetchAll :slight_smile:

	$stmt = $pdo1->prepare("SELECT * FROM fivemin_servers");
	$serverlist = $stmt->fetchAll();

Which got me all the rows.

Thank you both for the help!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.