Forum Moderators: coopster

Message Too Old, No Replies

Moving to PHP 7, MySQL growing pains

         

csdude55

11:11 pm on May 24, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The fact that PHP 7 doesn't make MySQL commands backwards compatible has been such a pain! I've spent the better part of the last year just trying to convert 100+ of my sites in my spare time >:-(

But I had a realization today... is there a reason to NOT simply add something like this to all of my variables scripts?

// at top of script that's included on every PHP page
if (phpversion() >= 7)
// not sure if I can put a single copy of mysql_functions.php on the server and
// let all of my shared hosting accounts access it? That'd be great; otherwise,
// I'd have a copy in the /public_html/ of each account
require_once '/home/mysql_functions.php';

// mysql_functions.php
function mysql_connect($host, $user, $pass) {
$GLOBALS['dbh'] = @mysqli_connect($host, $user, $pass);
return $GLOBALS['dbh'];
}

function mysql_select_db($db) {
global $dbh;
return mysqli_select_db($dbh, $db);
}

function mysql_query($query) {
global $dbh;
return mysqli_query($dbh, $query);
}

function mysql_real_escape_string($text) {
global $dbh;
return mysqli_real_escape_string($dbh, $text);
}

function mysql_fetch_array($text) {
return mysqli_fetch_array($text);
}

function mysql_fetch_assoc($text) {
return mysqli_fetch_assoc($text);
}

function mysql_fetch_row($text) {
return mysqli_fetch_row($text);
}

function mysql_num_rows($text) {
return mysqli_num_rows($text);
}

// and so on for any other MySQL function I use

I know it would probably make every page a tad slower, but at least I could upgrade my server and continue modifying each script as I have time. And then when MySQLi is eventually deprecated, it would be a snap to change this one script.

coopster

3:24 pm on Jun 4, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I created a DB class many years ago because of this very situation. You would be wise to create one for yourself and within that class you manage your DB functions within your own methods (class functions). I made mine a singleton instance too. Just to give you an idea of what I'm referring to here is a small sampling:
Database.php:
class Database { 
public function query($sql)
{
$result = $this->_db->query($sql);
if (!$result) {
$this->_error($sql); // internal error handler
}
$this->numRows = $this->_db->affected_rows;
return $result;
}

}

JAB Creations

8:22 am on Jun 17, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use Advanced Find and Replace. I use that stand alone program literally daily, mostly for finding. I use it extensively many years ago while updating my software to MySQLi.

John

JorgeV

9:43 am on Jun 17, 2020 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Hello,

I might remember wrongly, but I think that some MySQLi functions are not taking the exact same parameters than their equivalent without the "i". So be careful.