Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JOHNMAC's avatar

Fatal error: Uncaught Error: Call to a member function setFetchMode() on bool

Data is not fetching from db and shows error which is "Fatal error: Uncaught Error: Call to a member function setFetchMode() on bool in ...\practice.php:9 Stack trace: #0 {main} thrown in ...\practice.php on line 9"

code:

      <?php


    try{
    	$conn = new PDO("mysql: host = localhost; dbname = php_crud", 'root', '');

    	    $pdo = $conn->query("SELECT * FROM data");

    	    $pdo->setFetchMode(PDO:: FETCH_ASSOC);

    	while($row = $pdo->fetch()) {
	        	echo $row['email'] . "<br>";
        	}
    }

    catch(PDOException $e) {
	    echo "Error: " . $e->getMessage();
    }



?>
0 likes
4 replies
mabdullahsari's avatar

Place the setFetchMode above your query (select * from data).

You need to do the configuration before executing the query.

JOHNMAC's avatar

Notice: Undefined variable: pdo in ...\practice.php on line 7

Fatal error: Uncaught Error: Call to a member function setFetchMode() on null in

code:

  try{
	$conn = new PDO("mysql: host = localhost; dbname = php_crud", 'root', '');

	$pdo->setFetchMode(PDO:: FETCH_ASSOC);

	$pdo = $conn->query("SELECT * FROM data");

	

	while($row = $pdo->fetch()) {
		echo $row['email'] . "<br>";
	}
}
mabdullahsari's avatar

Yeah okay my bad, your variables names are confusing.

Are you sure your connection string is correct? query returns false probably.

JOHNMAC's avatar

yes

and in question all code has posted

Please or to participate in this conversation.