Get BD name on a mysql connection

Asked

Viewed 36 times

2

Good make a connection to BD mysql like this:

// Conecta-se ao banco do servidor
ini_set('default_charset', 'UTF-8');
$mysqli = mysqli_connect('127.0.0.1', 'root', '123', 'teste');
Query($mysqli, "SET NAMES utf8");

How can I get the comic’s name back teste through the variable $mysqli that contain the connection.

  • The last parameter passed to the "mysqli_connect" function is the name of the database, wouldn’t it be better if you store this is a global variable? So you wouldn’t need to make a query to know the name of the bank.

  • 1

    Good that Solve yes.

1 answer

4


Use the function database() Mysql it returns the name of the connection database.

SELECT database()

In php with Mysqli it looks like this:

$db = new mysqli('localhost', 'usuario', 'senha', 'db003-producao');
$result = $db->query('select database() as db');
$registro = $result->fetch_array();
echo $registro[0];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.