Get started with PHP-Fusion

PHP-Fusion 7 - MySQL to MySQLi

Hi,

I made some changes to my PHP-Fusion v7 and change it from MySQL to MySQLi (oop), as mysql has been deprecated and removed completly in PHP 7 + some other reasons.

There is no need to really talk that much, so here is code you need to change and where:


config.php
Add this line after line 3 (your port may be different, but as 3306 is default, it will work in most cases):
Code Gist:
$db_port = "3306";



maincore.php

Change line 101 and 102 from:
Code Gist:
$link = dbconnect($db_host, $db_user, $db_pass, $db_name);
unset($db_host, $db_user, $db_pass);
to
Code Gist:
$link = dbconnect($db_host, $db_user, $db_pass, $db_name, $db_port);
unset($db_host, $db_user, $db_pass, $db_port);


Change (And add) functions from line 235 (approx), to this:
Code Gist:
// MySQL database functions
function dbnew_result($res, $row, $field=0) {
   $res->data_seek($row);
   $datarow = $res->fetch_array();
   return $datarow[$field];
}

function db_lastid(){
   global $db_connect;
   return $db_connect->insert_id;
}

function db_affrows(){
   global $db_connect;
   return $db_connect->affected_rows;
}

function dbquery($query){
   global $db_connect, $mysql_queries_count, $mysql_queries_time; $mysql_queries_count++;

   $query_time = get_microtime();

   if (!$result = $db_connect->query($query)) {
      if ($db_connect->error) echo "Error: ".$db_connect->error."
";
      return FALSE;
   } else {
      $query_time = substr((START_TIME - $query_time),0,7);
      $mysql_queries_time[$mysql_queries_count] = array($query_time, $query);
      return $result;
   }
}

function dbcount($field, $table, $conditions = "") {
   global $db_connect, $mysql_queries_count, $mysql_queries_time; $mysql_queries_count++;

   $cond = $conditions ? " WHERE ".$conditions : "";
   $query_time = get_microtime();
   
   if (!$result = $db_connect->query("SELECT Count".$field." FROM ".$table.$cond)) {
      if ($db_connect->error) echo "Error: ".$db_connect->error."
";
      return FALSE;
   } else {
      $query_time = substr((get_microtime() - $query_time),0,7);
      $mysql_queries_time[$mysql_queries_count] = array($query_time, "SELECT COUNT".$field." FROM ".$table.$cond);
      $rows = dbnew_result($result, 0);
      return $rows;
   }
}

function dbresult($query, $row) {
   global $db_connect, $mysql_queries_count, $mysql_queries_time;

   $query_time = get_microtime();

   if (!$result = dbnew_result($query, $row)) {
      if ($db_connect->error) echo "Error: ".$db_connect->error."
";
      return FALSE;
   } else {
      $query_time = substr((START_TIME - $query_time),0,7);
      $mysql_queries_time[$mysql_queries_count] = array($query_time, $query);
      return $result;
   }
}

function dbrows($result){
   return $result->num_rows;
}

function dbarray($query){
   global $db_connect;
   if (!$result = $query->fetch_assoc()) {
      if ($db_connect->error) echo "Error: ".$db_connect->error."
";
      return FALSE;
   } else {
      return $result;
   }
}

function dbarraynum($query) {
   global $db_connect;
   if (!$result = $query->fetch_row()) {
      if ($db_connect->error) echo "Error: ".$db_connect->error."
";
      return FALSE;
   } else {
      return $result;
   }
}

function dbconnect($db_host, $db_user, $db_pass, $db_name, $db_port) {
   global $db_connect;

   // Create connection
   $db_connect = new mysqli($db_host, $db_user, $db_pass, $db_name, $db_port);
   unset($db_host, $db_port, $db_user, $db_pass);

   // Check connection
   if ($db_connect->connect_error) {
      die("Unable to establish connection to MySQL
".$db_connect->connect_error);
   }
}



print.php

Change last line (before ?>) from:
Code Gist:
mysql_close($db_connect);
to
Code Gist:
$db_connect->close();



forum/options.php
Change line 97 from:
Code Gist:
$del_posts = mysql_affected_rows();
to
Code Gist:
$del_posts = db_affrows();



forum/postnewthread.php
Change lines 114, 115 and 125 from:
Code Gist:
$thread_id = mysql_insert_id();
$post_id = mysql_insert_id();
$forum_poll_id = mysql_insert_id();
to
Code Gist:
$thread_id = db_lastid();
$post_id = db_lastid();
$forum_poll_id = db_lastid();



forum/postreply.php
Change line 80 from:
Code Gist:
$errorId = mysql_insert_id();
to
Code Gist:
$errorId = db_lastid();



forum/viewforum.php
Change line 102 and 104 from:
Code Gist:
$deleted_posts = mysql_affected_rows();
$deleted_threads = mysql_affected_rows();
to
Code Gist:
$deleted_posts = db_affrows();
$deleted_threads = db_affrows();



includes/error_handling_include.php
Change line 51 from:
Code Gist:
$errorId = mysql_insert_id();
to
Code Gist:
$errorId = db_lastid();



themes/templates/footer.php

Change last line (before ?>) from:
Code Gist:
mysql_close($db_connect);
to
Code Gist:
$db_connect->close();



Also you might have to do similar changes to plugins if they used mysql_insert_id();, mysql_affected_rows(); or mysql_close(); (ie. bad_bots uses mysql_close();) as I had to do with my own infusions I wrote, which use ajax a lot and last inset ID is damn useful when dealing with ajax row inserting and immediate deletion after that (thats why I made them their own functions (no need for globals).
If they used own queries (no dbquery(); / dbarray(); etc.) you will have to change that too, but I actually never seen that in any infusion.

I hope this has helped you.


Credits: Original code I edited is here. Thanks for that article, saved me some time and pushed me over the edge to actually do this.
Posted by MiChAeLoKGB on January 11 2016 19:15:37
3 Comments · 7387 Reads · Print
Comments
#1 | MiChAeLoKGB on Jan 11 2016 at 20:35:58
I forgot one more step. To be sure, Ill go trough every file and find if there are more missing edits. Sorry


administration/custom_pages.php
Change line 78 from:
Code Gist:

$page_id = mysql_insert_id();
to
Code Gist:

$page_id = db_lastid();



administration/phpinfo.php
Change line 42 from:
Code Gist:

$phpinfo .= "<tr>\n<td class='tbl2' style='width:20%'>".$locale['406']."</td><td class='tbl2' style='text-align:right'>".mysql_get_server_info()."</td></tr>\n";
to
Code Gist:

$phpinfo .= "<tr>\n<td class='tbl2' style='width:20%'>".$locale['406']."</td><td class='tbl2' style='text-align:right'>".$db_connect->server_info."</td></tr>\n";
#2 | Systemweb on Jun 01 2016 at 12:39:54
More changes needed for user registration / if email verification required.
The function "unserialize" has new restrictions in PHP 7, so we can use the same new way as in Fusion v9:
register.php
Change line 50 from
Code Gist:

$user_info = unserializeFix(stripslashes($data['user_info']));
to
Code Gist:

$user_info = unserialize(base64_decode($data['user_info']));

After this step you can complete remove no more used function in lines 42-45:
Code Gist:

function unserializeFix($var) {
   $var = preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $var);
   return unserialize($var);
}


includes/classes/UserFieldsInput.class.php
Change line 585 from
Code Gist:

$userInfo = addslash($userInfo);
to
Code Gist:

$userInfo = base64_encode($userInfo);


Btw.
If full error_reporting is enabled, no captcha image will be shown on PHP 7. Thats why the classes includes functions with the same name (deprecated).
To fix that for now, set error_reporting to 0.

If you made all the changes in this article and its comments your Fusion v7 is running under PHP 7.0.x
#3 | Systemweb on Jun 14 2016 at 20:27:37
The line
Code Gist:

mysql_close($db_connect);
should be changed to
Code Gist:

$db_connect->close();
in these files too:
maintenance.php
setuser.php
admin/go.php

In admin/db_backup.php (and maybe in any other script?) there is an old no more supported function called "mysql_field_name".
To fix this, just add this code anywhere in maincore.php, eg. before "function db_connect(...)":
Code Gist:

// added for compatibility to older mysql:
if(!function_exists("mysql_field_name")) {
   function mysql_field_name($result, $field_offset) {
      $properties = mysqli_fetch_field_direct($result, $field_offset);
      return is_object($properties) ? $properties->name : null;
   }
}
Post Comment
Please Login to Post a Comment.
Ratings
Rating is available to Members only.

Please login or register to vote.

Awesome! Awesome! 0% [No Votes]
Very Good Very Good 100% [1 Vote]
Good Good 0% [No Votes]
Average Average 0% [No Votes]
Poor Poor 0% [No Votes]