-2

I have developed a web based member management tool for my sailing club few years ago and used some free script (a class) that handles database connection and operations on database. The class (connection.php) is invoked by the following code in a required file "settings.php":

$db = new db ( DBUSER, DBPASS, DATABASE, HOSTNAME );

and then is used throughout the site for handling database related requests. It is working fine in PHP 5.3 but due to some security concerns the site has to be upgraded to min 5.4. Unfortunately the script in PHP 5.4 fails to connect to database. I will include the script below (for brevity I show only couple of functions in the class that should give the idea):

// ==================================================================
//  The Main Class

class db {

    // ==================================================================
    //  DB Constructor - connects to the server and selects a database

    function db($dbuser, $dbpassword, $dbname, $dbhost)
    {

        $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword);

        if ( ! $this->dbh )
        {
            $this->print_error("<ol><b>Error establishing a database connection!</b><li>Are you sure you have the correct user/password?<li>Are you sure that you have typed the correct hostname?<li>Are you sure that the database server is running?</ol>");
        }


        $this->select($dbname);

    }

    // ==================================================================
    //  Select a DB (if another one needs to be selected)

    function select($db)
    {
        if ( !@mysql_select_db($db,$this->dbh))
        {
            $this->print_error("<ol><b>Error selecting database <u>$db</u>!</b><li>Are you sure it exists?<li>Are you sure there is a valid database connection?</ol>");
        }
    }

When trying to display a page in PHP 5.4 I get the following output:

dbh = @mysql_connect($dbhost,$dbuser,$dbpassword); if ( ! $this->dbh ) { $this->print_error(" Error establishing a database connection! Are you sure you have the correct user/password? Are you sure that you have typed the correct hostname? Are you sure that the database server is running? "); } $this->select($dbname); } // ================================================================== // Select a DB (if another one needs to be selected) function select($db) { if ( !@mysql_select_db($db,$this->dbh)) { $this->print_error(" Error selecting database $db! Are you sure it exists?he rest of the pages Are you sure there is a valid database connection?

My aim is to rewrite the class db without touching the rest of the pages. I was trying to use mysqli instead of mysql but that made no difference. I'm using mainly procedural php in my programming and am not to good with object oriented.

My question is: what approach should I take to solve my problem. Could you put me in some direction...

Voytek
  • 37
  • 1
  • 5

4 Answers4

0

This function appears to be deprecated as of PHP 5.5

http://php.net/manual/en/function.mysql-connect.php

0

If you have access to the php.ini file after the upgrade to 5.4 I would check to make sure the extension you are trying to use is unchecked in the windows extensions settings. If you pay for hosting, their tech support may be able to help if you have a lot of patience.

jguess
  • 16
0

Thanks everybody for your contributions.

I've identified a simple (but major) problem with file connection.php containing Class db. It was using short version of php identifier: <? rather than <?php, which was causing fatal error of not finding the Class.

With this out of the way I am back to my original issue of deprecated functions not working correctly in php 5.5 but need to do more research before (eventually) asking another question.

Thanks again.

Voytek
  • 37
  • 1
  • 5
0

@Qchmqs Jan 8 at 7:28

you should be using PDO anyway

No!

You should be using PDO OR MySQLi

Balance please! there are plenty of well learned folk who advocate MySQLi for some situations, its a matter of judgement etc.

Jonnie
  • 1
  • 1