-4

My host recently upgrade the PHP version and a certain part of my website now shows the following error:

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in url/structure/here on line 49

That is referencing the below code:

function DBConnect() {  
    $this->connectCount ++;
    //echo "$this->connectCount<br>";

    if ($this->dbType == 'mysql') {
        $dbConnect = mysql_connect($this->dbHost, $this->dbUser, $this->dbPasswd) or die ("MySql Connection Failed: " . mysql_error());
        mysql_select_db($this->dbName, $dbConnect);
    }

    if ($this->dbType == 'postgresql') {
        $dbConnect = pg_connect("host=$this->dbHost port=$this->dbPort dbname=$this->dbName user=$this->dbUser password=$this->dbPasswd") or die ("PostgreSQL Connection Failed: " . pg_errormessage($dbConnect));
        //$dbConnect = pg_pconnect("host=$this->dbHost port=$this->dbPort dbname=$this->dbName user=$this->dbUser password=$this->dbPasswd") or die ("PostgreSQL Connection Failed: " . pg_errormessage($dbConnect));
    }

    return $dbConnect;
}

I'm aware the fact that this is because the current way my site connects to MYSQL is now outdated in the new version of PHP but does anyone know how I would update the above code to make this work?

  • The easiest way is probably to use `mysqli_` which has very similarly named functions. Check out the [docs](http://php.net/manual/en/book.mysqli.php). – vch Aug 17 '14 at 21:34
  • http://php.net/manual/en/function.mysql-connect.php – PeeHaa Aug 17 '14 at 21:34
  • I would go ahead and migrate to PDO: http://www.sitepoint.com/migrate-from-the-mysql-extension-to-pdo/ – casraf Aug 17 '14 at 21:34

1 Answers1

0

The easier way is to use mysqli_connect(). The syntax is very similar to what you would had with mysql_connect(),which means the changes in your code will be minor and easy to make.

Pdo would be the safest, but if you are trying to get you site back on quickly, the mysqli_* commands will achieve that.

Google (or check on stackoverflow) mysql vs mysqli. You ll find plenty of examples.

Hope this helps.

Good luck

--

Sorry after re-reading i see you asked what needs to be change on your source code. Afraid i cannot help right now as i am responding from a mobile phone :(

Matt Alice
  • 41
  • 3