Tuesday, October 8, 2019

A MySQL Wrapper for mysqli - old skool mysql

A Wrapper for mysqli to Enable Old Skool Mysql


Get this cool mysql wrapper from github to retrofit your old skool mysql / php code to work on php7 where mysql has been replaced by mysqli.   If you are seeing this error in your old php5 code, and you have calls to old mysql functions all of the place, you can quickly retrofit your programs.


1
2
3
4
5
6
7
PHP Fatal error:  Uncaught Error: Call to undefined function mysql_connect() in /opt/bitnami/apache2/htdocs/lib/class.db.php:29
Stack trace:
#0 /opt/bitnami/apache2/htdocs/lib/class.db.php(24): Db->connect()
#1 /opt/bitnami/apache2/htdocs/common/config.php(21): Db->Db('localhost', 'dbadmin', 'Sick1234', 'demodatabase')
#2 /opt/bitnami/apache2/htdocs/mysql-test.php(6): include('/opt/bitnami/ap...')
#3 {main}
  thrown in /opt/bitnami/apache2/htdocs/lib/class.db.php on line 29

where line 29 has this bit of code

 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
                /* Establish connection with the server & database and return resource on success else boolean */
                function connect() {
                        $this->linkid = @mysql_connect($this->hostname, $this->username, $this->password);
                        if(!$this->linkid){
                                $this->error("Could not connect with server!".$this->hostname);
                                return false;
                        } else {
                                $check = @mysql_select_db($this->databasename, $this->linkid);
                                if ($check) {
                                        return $this->linkid;
                                } else {
                                        $this->error("Could not connect with database!!");
                                        return false;
                                }
                        }
                }


logon to your webserver using SSH, and then on your command line shell, in your webroot directory, do this:

mkdir ext
cd ext
git clone https://github.com/e-sites/php-mysql-mysqli-wrapper.git
cp ./ext/php-mysql-mysqli-wrapper/mysql.php ./lib

You will have fetched a file called mysql.php
now copy it to ./lib
add the first line to your php code:

....
include("./lib/mysql.php");   /* this is what you got from git */;
....
include("./common/lib.php");  /* my custom libs */
include("./lib/class.db.php");
include("./common/config.php");



No comments:

Post a Comment