11.06.2010

SQLSTATE[HY000]: General error: could not call class constructor

SQLSTATE[HY000]: General error: could not call class constructor

This rather vague error message has been popping up in some of my unit tests lately.  It's a PHP error in PDO querying MySQL that happens when you're using the setFetchMode method of the PDOStatement.  If you use the fetchMode of "FETCH_CLASS" you can get this error in one of three ways:

  • The class you specified has not been included/required so when PDO gets results, it cannot create an object of the class you specified since it doesn't have that class definition.
  • Properties on the class you specified have been marked as protected or private and there aren't any setters for them so PDO can't set the properties on the new object.
  • You used the optional third argument "ctorargs" to give constructor arguments on your new object however your arguments are incorrect and don't match those on the class.
Hope this helps other poor souls out there trying to figure this error out.


11.05.2010

"show tables not like" in mysql

I was looking for the tables in my database not starting with "p_".  MySQL doesn't let you execute this command:

show tables not like "p_%";

Which is odd considering it will allow "like" here:

show tables like "p_%";

Instead, you have to use the information schema like this:

SELECT table_name FROM information_schema.tables WHERE table_schema = 'mydb' AND table_name NOT LIKE 'p_%';