Add missing wpsql_errno in PG4WP plugin

Today I tested updating our website based on WordPress using a Postgresql database through the PG4WP driver. Unfortunately, it seems that the author doesn’t update it anymore since 2012. Version 3.9.1 of WordPress makes new functions calls to the database that are not handled by the original driver. I needed to tweak 2 parts of the code to make it work:

1) Sorving the “Warning: Missing argument 3 for wpsql_result()”

Edit the function wp-content/pg4wp/driver_pgsql.php on line 58 with :

function wpsql_result($result, $i, $fieldname = null) {
		if (is_resource($result)) {
			if ($fieldname) {
				return pg_fetch_result($result, $i, $fieldname);
			} else {
				return pg_fetch_result($result, $i);
			}
		}
	}

2) Solving the error “PHP Fatal error:  Call to undefined function wpsql_errno() in /path/to/your/site/wp-content/pg4wp/core.php(32) : eval()’d code on line 1531”

Add at the end of your wp-content/pg4wp/driver_pgsql.php:

function wpsql_errno( $connection) {
 $result = pg_get_result($connection);
 $result_status = pg_result_status($result);
 return pg_result_error_field($result_status, PGSQL_DIAG_SQLSTATE);
 }

Source 1: http://wordpress.org/support/topic/not-working-with-39-44?replies=7
Source 2: http://stackoverflow.com/questions/6589481/postgresql-equivalent-of-mysql-errno

7 thoughts on “Add missing wpsql_errno in PG4WP plugin

  1. I’m trying to install wordpress-3.9.3 using PGWP-1.3.1, and I’m also seeing this error:

    `
    PHP Warning: pg_query(): Query failed: ERROR: missing FROM-clause entry for table “session”\nLINE 1: SELECT @@SESSION.sql_mode\n ^ in /var/www/html/year6/wp-content/pg4wp/driver_pgsql.php on line 136
    `
    Any suggestions on how to solve it?

  2. Disclaimer: I’m not using WordPress 3.9.3 by the moment, this is a theoretical solution based on my lecture of the code and generated error. Use it at your own risk. Use a test setup and test all functions you need before deploying this patch in a production!

    SESSION.sql_mode is a way of getting the settings of MySQL. This won’t work on Postgresql.
    You can try to bypass this check by modifying the file wp-includes/wp-db.php line 737:

    			if ( $this->use_mysqli ) {
    				$res = mysqli_query( $this->dbh, 'SELECT @@SESSION.sql_mode' );
    			} else {
    				$res = mysql_query( 'SELECT @@SESSION.sql_mode', $this->dbh );
    			}
    

    Comment this code and change it by $res = “”; as following:

    /*			if ( $this->use_mysqli ) {
    				$res = mysqli_query( $this->dbh, 'SELECT @@SESSION.sql_mode' );
    			} else {
    				$res = mysql_query( 'SELECT @@SESSION.sql_mode', $this->dbh );
    			}*/
    			$res = "";
    

    Let me know if this save your problem.

    Edit: What I proposed is a “quick & dirty” hack. If it works, the clean way to deal with it is to modify the pg driver so it ignores the “SELECT @@SESSION.sql_mode” query.

  3. Thanks @Vitorio. I made the changes that you suggested, and I’m no longer seeing the error. However, I’m seeing “Error establishing a database connection” in my web browser, with no errors logged anywhere else that I can find.

Leave a comment