php: getting database name from own mysli class
Asked Answered
U

2

5

i made a wrapper class for mysqli with the following syntax:

class mydb extends mysqli
{
     ....
}

i'm creating instances like the following:

$conn = new mydb($host $username , $pass, $dbname);

i'm wondering - how can i access $dbname from inside the class? i'm looking for an mysqli equivalent for the mysql_db_name() function.

Underfeed answered 24/8, 2013 at 0:15 Comment(2)
That looks like an unecessary wrapper to meTyishatyke
If your unsure on how to approach a Wrapper, then study one that's already in use. github.com/ajillion/PHP-MySQLi-Database-ClassEcg
M
5

You could always make your own class that has a field variable with a reference to a mysqli object and database name.

class MyDatabase {

    public $dbi;
    public $dbname;

    function __construct($host, $user, $pass, $db) {
        $this->dbi = new mysqli($host, $user, $pass, $db);
        $this->dbname = $db;
    }

}

$mydb = new MyDatabase($host, $user, $pass, $db);

//Access db name that was stored during connection
echo $mydb->dbname;
Maestricht answered 24/8, 2013 at 0:38 Comment(0)
J
20

You can use this SQL query from your own class:

SELECT database() AS the_db

That should show you the current DB you have selected.

Jarv answered 24/8, 2013 at 0:19 Comment(2)
this should be the best answer in my opinionApplecart
This is the best answer.Lovering
M
5

You could always make your own class that has a field variable with a reference to a mysqli object and database name.

class MyDatabase {

    public $dbi;
    public $dbname;

    function __construct($host, $user, $pass, $db) {
        $this->dbi = new mysqli($host, $user, $pass, $db);
        $this->dbname = $db;
    }

}

$mydb = new MyDatabase($host, $user, $pass, $db);

//Access db name that was stored during connection
echo $mydb->dbname;
Maestricht answered 24/8, 2013 at 0:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.