WordPress Link / Blogroll error
Friday, September 14th, 2007One day I noted that my blog was showing an SQL-error in the LINKS section. What a surprise? How come?
I had no idea what action caused this fault, was it a stupid edit somewhere, a bad internet connection while updating my blog or a half succeeded hacking attempt?
SQL-syntax errror
The error was this:
WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ” at line 1]
SELECT cat_id, cat_name FROM
And it appeared under the Link/Blogroll section.
What could have caused the problem?
Well, what I knew was that I had updated MyBelovedPHP to the newest version of WordPress 2.2 a few weeks ago. The update seemed to work without problems, but probably I hadn’t scrutinized my blog for a 100%.
I could remember that 2.2 was only suited for PHP 5 version, PHP 4 was no longer supported. OK, but I had PHP 5 on my server. What then?
SQL is database related, what were the changes form WordPress 2.0 tot 2.2. And what didn’t change?
Of course my theme was still the same, that wasn’t updated! Could there be somewhere an issue introduced?
Why was the SQL syntax truncated? It ended at FROM. Where was the tablename and the rest of the query?
Finding the solution
I decided to examine my theme files looking for the Links/Blogroll part and stumbled upon an query in the Links section in my sidebar.php. I opened phpMyAdmin and look for the table, it was not in the database! It seems that WordPress 2.2 joined a few tables, and referencing a non-existing table in a query will of course cause an error. This is the query:
< ?php
$link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM $wpdb->linkcategories ");
foreach ($link_cats as $link_cat) {
?>
This table disappeared: linkcategories
A quick look into my database showed a table called: categories.
Fixing the code
I changed the code to:
< ?php
$link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM $wpdb->categories");
foreach ($link_cats as $link_cat) {
?>
It helped, the error-message disappeared, but it showed a lot of empty categories. So i changed the code to:
< ?php
$link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM $wpdb->categories where link_count > '0'");
foreach ($link_cats as $link_cat) {
?>
That was all. Now it’s working again!