SMF 2.0 with PHP 7

SMF does not officially support running version 2.0.x with PHP 7.0.  This is due to PHP removing the mysql library in favor of more secure MySQLi library.  To get around this if you have root access to your server, you could manually build in the old mysql library functions.  Or you know, build compatibility functions.  I’m releasing this as a proof of concept that this works, I highly suggest migrating all code toMySQLi functions rather than using this, but it provides a simple path that allows you to upgrade PHP and enough time to migrate your code base over.

In $sourcedir/Subs-Db-mysql.php Find:

	// Map some database specific functions, only do this once.

Add before this:

	global $sourcedir;
	if (@version_compare(PHP_VERSION, '7.0') >= 0)
		require_once($sourcedir . '/Subs-Compat.php');

Now find:

	if (!is_resource($connection))
		db_fatal_error();

Replace with:

	if (!is_resource($connection) && !is_object($connection))
		db_fatal_error();

In $sourcedir/Subs-Compat.php at the end before ?> add:

// Compatibility where MySQL functions don't exist.
if (!function_exists('mysql_connect'))
{
	function mysql_connect($host, $username, $password){return mysqli_connect($host, $username, $password);}
	function mysql_pconnect($host, $username, $password){return mysqli_connect('p:' . $host, $username, $password);}
	function mysql_real_escape_string($string, $connection = null){return mysqli_real_escape_string($connection, $string);}
	function mysql_select_db($database, $resource = null){return mysqli_select_db($resource, $database);}
	function mysql_fetch_row($resource = null){return mysqli_fetch_row($resource);}
	function mysql_free_result($resource = null){return mysqli_free_result($resource);}
	function mysql_num_rows($resource = null){return mysqli_num_rows($resource);}
	function mysql_data_seek($resource = null, $row){return mysqli_data_seek($resource, $row);}
	function mysql_num_fields($resource = null, $row){return mysqli_num_fields($resource, $row);}
	function mysql_get_server_info($resource = null){return mysqli_get_server_info($resource);}
	function mysql_error($resource = null){return mysqli_error($resource);}
	function mysql_errno($resource = null){return mysqli_errno($resource);}
	function mysql_query($query, $resource = null){return mysqli_query($resource, $query);}
	function mysql_unbuffered_query($query, $resource = null){return mysqli_query($resource, $query, MYSQLI_USE_RESULT);}
	function mysql_affected_rows($resource = null){return mysqli_affected_rows($resource);}
	function mysql_insert_id($resource = null){return mysqli_insert_id($resource);}
	function mysql_fetch_assoc($resource = null){return mysqli_fetch_assoc($resource);}
}

 

This should allow SMF to run just fine.  These are all the functions SMF calls from it’s database abstraction layer and thus should work.  Anyone using mysql functions outside of SMF’s database abstraction layer may need to add in additional functions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.