PDO query only returning one result

#1

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");

	$stmt->execute();

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

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


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

)

Could someone tell me what I’m doing wrong?

Thanks for your time!

#2

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.

http://php.net/manual/en/pdostatement.fetch.php

#3

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…

#4

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");
	$stmt->execute();
	$serverlist = $stmt->fetchAll();

Which got me all the rows.

Thank you both for the help!

1 Like
closed #5

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