0

This is long-winded, but my current setup is a WordPress multisite that is distributed over 256 databases (performance reasons). When sites are generated, a script auto-populated the WP tables needed onto a randomly selected database. The science is beyond my understanding completely.

Anyways, I have a need to alter data on a table for one of my sites. I need to find out which database its table reside. A long while ago, I had success with this:

<?php
    $query="select database() AS <code>db</code>";
    $result=mysql_query($query);
    $row = mysql_fetch_assoc($result);
        echo 'database: '.$row['db'].'<p>';
?>

That no longer seems to function and looks like it has deprecated functions. Any help would be greatly appreciated. Thanks!

M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
NW Tech
  • 328
  • 3
  • 16
  • 2
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). *They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation)*. Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 06 '14 at 19:11
  • 2
    Thanks @JayBlanchard...I came to that conclusion and mentioned it already "That no longer seems to function and looks like it has deprecated functions." – NW Tech Oct 06 '14 at 19:14

1 Answers1

1

If you're using mysqli_ you can do this -

if ($result = $mysqli->query("SELECT DATABASE()")) {
    $row = $result->fetch_row();
    printf("Default database is %s.\n", $row[0]);
    $result->close();
}

That returns the name of the default database for this connection.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119