Get Database Name

How i get the currently connected database name?

i used this




$currentdb  = explode('=', Yii::app()->db->connectionString);

echo $currentdb[2];



any other simple methods?

1 Like

That’s probably as simple as any if you don’t want to query the database itself and you can be confident that your connection string will always be configured the same way. If you wanted to make it a bit more robust, you could use a regex to pull out the string between “dbname=” and the next “;” character or the end of the string.




preg_match("/dbname=([^;]*)/", Yii::app()->db->connectionString, $matches);

return $matches[1];



Show off ;)

just your answer in a condensed form

ok… thanks for the suggestion. :)

ok. thanks . so there is no direct method, right? only from Yii::app()->db->connectionString ! :)

1 Like

thank you, helpful