-1

I'm wondering if it's possible to convert a database connection object to an array in PHP? I've been researching, it seems you can cast an object as an array, but it doesn't seem to work for a connection object. Here's my script:

<?php       

$dbHost="host";
$dbUser="user";
$dbPass="";
$dbName="root";

$dbConn = mysqli_connect($dbHost,$dbUser,$dbPass,$dbName);

$dbParts = (array) $dbConn;

for ($i=0;$i <=3; $i++){

echo $dbParts[$i];

}
?>

Any help would be greatly appreciated!

**[EDIT]**Why I need to access the db connection as an array is because I need to be able to call certain urls in a script, based on what database I'm connected to, as each environment has a different database.

AyeLetsGetCoding
  • 47
  • 1
  • 1
  • 5
  • 1
    more to the point WHY would you ever want to ?? –  Dec 05 '17 at 20:00
  • 1
    Sort of... Your use case is needed to answer the question. e.g. what do you want the output of this code to be? – Dan Dec 05 '17 at 20:01
  • "but it doesn't seem to work" _In what way_ doesn't it work? Are you getting an error? Are you getting unexpected output? Have you checked to see if your connection was even successful? – Patrick Q Dec 05 '17 at 20:04
  • 3
    now you have added why .. cant you just use `$dbName` ?? –  Dec 05 '17 at 20:05
  • I cannot, the code above is for test purposes, the actual code I will need accesses a db that is set in another script that has no variable for the dbName, the name is hardcoded. – AyeLetsGetCoding Dec 05 '17 at 20:14
  • if you parse the `$dbConn` around you can parse `$dbName` as well. you do seem insistent in doing this a weird way. –  Dec 05 '17 at 20:23

2 Answers2

2

Build your own array:

$db['localhost'] = array('host' => 'localhost',
                         'user' => 'root',
                         'pass' => '****',
                         'name' => 'database1'); 

Then if needed add the connection to it:

$db['localhost']['conn'] = mysqli_connect($db['localhost']['host'],
                                          $db['localhost']['user'],
                                          $db['localhost']['pass'],
                                          $db['localhost']['name']);

Other possibilities depending on your needs; define the database as the lookup key:

$db['database1'] = array('host' => 'localhost',
                         'user' => 'root',
                         'pass' => '****',
                         'name' => 'database1'); 

Or define the environment:

$db['production'] = array('host' => 'localhost',
                          'user' => 'root',
                          'pass' => '****',
                          'name' => 'database1'); 
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

Well after a little digging I found this ( Tested ). It relies on you having access to $dbConn.

You can get the database name being used, using something like...

$result=mysqli_query($dbConn,'SELECT DATABASE() as dbName');
$rows = mysqli_fetch_assoc($result);
var_dump($rows);

which you'd probably turn into a function to make it useful along the lines of...

function getDatabaseName($databaseConnection){
    $result=mysqli_query($databaseConnection,'SELECT DATABASE() as dbName');
    $row = mysqli_fetch_assoc($result);
    return $row['dbName'];
//    var_dump($rows);
}

$currentDatabase = getDatabaseName($dbConn);
echo $currentDatabase;

You'll have to play with this to suit...

TimBrownlaw
  • 5,457
  • 3
  • 24
  • 28