Call to undefined method PDOStatement::setFetchMode()
Asked Answered
M

1

6

I'm working with drupal 7, on PHP 7, on Xampp on Windows, and suddenly I start getting the following error:

Call to undefined method DatabaseStatementBase::setFetchMode()

Where DatabaseStatementBase extends PDOStatement directly. When reducing the code to the following minimum:

<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', 'test', 'test');
$pdostatement = $dbh->prepare('SELECT * FROM items WHERE id=?');
$pdostatement->setFetchMode(PDO::FETCH_CLASS);
$success = $pdostatement->execute([1]);
// do stuff...

It still throws an error on the line regarding setFetchMode. When I comment that line out, no error is thrown, but I get an associative array instead of an object — not what drupal expects. Especially since setFetchMode should exist (see http://php.net/manual/en/pdostatement.setfetchmode.php)

Finally, when I then try to find the methods of the $pdostatement using reflection, I get garbage for some of the names — or, more accurately, the name seems about 1.5MB long and contains a lot of unreadable characters and some of the method names, as if an entire DLL was loaded in there or something. Here's an example of what var_dump (php7 & xdebug) make of it:

object(ReflectionMethod)[17]
    public 'name' => string '����&������p�aZ������������    ���bindParam�������{�nZ���������������setAttribute����f�kZ����������j����FETCH_ORI_FIRST�a�pZ���������q��
    ���CURSOR_SCROLL���l�}Z���������������fetchColumn������zZ��������������wph�����&��������Z���������������debugDumpParams���Z��������.�����children����������Z������������wphX����&��������Z��������(��
    ���nextrowset��������Z������������
    ���__toString������ ��Z������������wph(����&������4��Z��������'... (length=1752201104)
    public 'class' => string 'PDOStatement' (length=12)

How can I fix this?

Mansfield answered 24/11, 2017 at 9:23 Comment(3)
Sounds weird. Please elaborate on the environment - software used, versions, etc. As a workaround you can always set the default fetch mode or use fetchObject instead of fetch()Doug
besides, you don't need reflection to find the methods, just use get_class_methods()Doug
Ah, right, get_class_methods yiels the same result. However, using fetchObject isn't a valid workaround — remember, I'm working with drupal here, I'd have to change a lot of code. Besides, production still works.Mansfield
M
1

The solution was: try turning it off and on again — in this case the Apache server. Apparently something got corrupted in memory, and restarting the server fixed it.

I assumed this was already done by me shutting down my computer yesterday, after the problem appeared, and booting it again this morning. Now I know: windows 8 and up use a hybrid shutdown/hibernate instead of a real shutdown, so your apache service doesn't really get restarted if you think you restarted your PC.

Somehow this makes me feel so dumb...

Mansfield answered 24/11, 2017 at 10:40 Comment(2)
do you have opcache active?Handgrip
I think so, there's one line for it in my phpinfo() output.Mansfield

© 2022 - 2024 — McMap. All rights reserved.