One of the most common errors that programmers and web developers will see is the PHP error describing an attempt to Call to a member function on a non-object. More often than not, this error gets thrown when trying to communicate with a MySQL database using PHP classes like PDO or MySQLi, which are two common ways to interact with a MySQL database via PHP. Most of the time an error like this will get thrown because an invalid MySQL query was used to query the database, resulting in the MySQL result object not being an object at all.

However, what happens when one gets the “Call to a member function on a non-object” error EVEN AFTER the MySQL query has been confirmed to be a valid one?

We had an issue with something like this not too long ago which involved a database of typos, where the two main columns were “wrong” and “right” to denote which word was the misspelled one and which word was it’s corresponding, correctly-spelled counterpart. After verifying that we were in fact passing a valid query to our MySQL PHP functions, it became clear that we had unwittingly stumbled upon one of MySQL’s dreaded reserved words. MySQL needs certain words to be reserved so that they can serve a special purpose as an identifier or function name–basically a special word that helps MySQL function how it is supposed to.

We had an issue with something like this not too long ago which involved a database of typos, where the two main columns were “wrong” and “right” to denote which word was the misspelled one and which word was it’s corresponding, correctly-spelled counterpart. After verifying that we were in fact passing a valid query to our MySQL PHP functions, it became clear that we had unwittingly stumbled upon one of MySQL’s dreaded reserved words. MySQL needs certain words to be reserved so that they can serve a special purpose as an identifier or function name–basically a special word that helps MySQL function how it is supposed to.

Incidentally, we had decided to name one of our columns “right,” which turned out to be one of MySQL’s reserved keywords. Once we changed the column name to “right_word,” something that was definitely not a reserved keyword, the “member function” error disappeared and our PHP script immediately ran as intended. You can find a list of MySQL Keywords and Reserved Words on the MySQL website. Other MySQL reserved words that will likely cause problems if you use them as column names are “GROUP,” “ALL,” “CHARACTER,” “RANGE,” and “WHEN.” Being aware of these reserved words and keeping a reference handy could easily save you on unnecessary debugging time.

Mission Bay Media is a Website Development company based out of sunny San Diego, California. For more information about this post, or about our company and what we can do for you, feel free to email us at info@missionbaymedia.com.