-1

I've been forced to convert my website written for PHP 5 into 7.3. Say in the following (old) function:

function logMySQLError($err_line)
{
    $err_no = @mysql_errno();
    $err_desc = @mysql_error();
    $err_time = date("n/j/Y, G:i:s", getLocalDateTimw());

    postError("$err_time - MySQL #$err_no '$err_desc', line:$err_line");
}

That could be called throughout the website as such:

if(!$result)
{
    logMySQLError(__LINE__);
}

Now in 7.3 I have to add 'i' to make this work ;) but then it also requires $link parameter:

function logMySQLError($err_line)
{
    $err_no = mysqli_errno(/* $link */);
    $err_desc = mysqli_error(/* $link */);
    $err_time = date("n/j/Y, G:i:s", getLocalDateTimw());

    postError("$err_time - MySQL #$err_no '$err_desc', line:$err_line");
}

How do I get the last used $link from the previous mysqli_connect() call without passing it explicitly into this function? (Like it used to work.)

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • Is there any global `$connection` variable? – nice_dev Nov 21 '19 at 07:21
  • [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Nov 25 '19 at 09:46
  • for this particular case of the error logging you don't really need such a function at all. Errors could (and should) be logged automatically by a site-wide handler. however I understand that you need this link in many other places as well. google for mysqli shim replacement – Your Common Sense Nov 25 '19 at 09:53

2 Answers2

0

You can use the class approach, as far I saw you have only functions which probably seems like you're using the basic procedural programming, for this approach maybe it'll be better you swtich for a OOP approach.

I'll give a class for an example:

class DB {

    protected $db_name = 'databasename';
    protected $db_user = 'databaseuser';
    protected $db_pass = 'databasepassword';
    protected $db_host = 'databasehost';
    protected $connect_db = null;

    // Open a connection to the database.
    // Make sure this is called on every page that needs to use the database.

    public function connect() {

        $this->connect_db = new mysqli( $this->db_host, $this->db_user, $this->db_pass, $this->db_name );

        if ( mysqli_connect_errno() ) {
            printf("Connection failed: %s\
", mysqli_connect_error());
            exit();
        }
        return true;

    }

    /** Here's your new function **/
    public function logMySQLError($err_line)
    {
        $err_no = mysqli_errno($this->connect_db);
        $err_desc = mysqli_error($this->connect_db);
        $err_time = date("n/j/Y, G:i:s", getLocalDateTimw());

        postError("$err_time - MySQL #$err_no '$err_desc', line:$err_line");
    }

}
  • 1
    Thanks. I'm trying to convert quite a large website (that was written some time ago) with the minimum damage to it (i.e. a few changes to the existing code as possible.) And if I went with what you suggested, passing the $link inside that function as a parameter would be a much simpler way. But otherwise, I agree that I would do what you suggested if I was writing it from scratch. – c00000fd Nov 21 '19 at 07:10
  • As far I know you can make you $link connection inside the $_GLOBALS on PHP and retrieve it on any part of code while running, but it's not advisable and not even a good practice and this is it'll not avoid you to rewrite some parts of code. The class approach it'll be better for you, you can simply instantiate it anywhere and use it's a reference while your thread is running and the damage it'll be harmless if you implement it correctly. – Fabio William Conceição Nov 21 '19 at 07:29
  • 2
    Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) – Dharman Nov 25 '19 at 09:45
-1

Use class + construct.

require_once("config.php");

class MySQLDatabase {

    private $connection;

    function __construct() {
        $this->open_connection();   
    }

    public function open_connection() {

    $this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
        if(mysqli_connect_errno()) {
            $msg = "Database connection failed: ";
            $msg .= mysqli_connect_error();
            $msg .= " (" . mysqli_connect_errno() . ")";
            exit($msg);
        }
        // Change character set to utf8
        mysqli_set_charset($this->connection,"utf8");

      }

    public function close_connection() {
        if(isset($this->connection)) {
          mysqli_close($this->connection);
          unset($this->connection);
            }
          }
}

$database = new MySQLDatabase();
$db =& $database;
Mantykora 7
  • 173
  • 2
  • 8
  • 19