简体   繁体   中英

How can I print the connected database information (such as db name) in PHP?

I am trying to execute an SQL query in PHP but even though the DB has been connected it returns 0 rows, but when I try to execute the same query in MSSQL it returns a row.

I have tried the one below but it returns just Boolean false:

$selecteddb = $dbi->query('select database()');
var_dump($selecteddb);

Here is how I execute my query:

$user_sql = "SELECT * from users where username='myusername' AND password='pass123'";
$user_res = $dbi->prepare($user_sql);
$user_res->execute();
$user_data = $user_res->fetchAll();
$user_unum = count($user_data);

I just want to verify that I am actually connected to the right db.

Update:

I will need an equivalent of mysql_db_name to print the db name. This one is deprecated and does not work in php 7.

尝试 'Select db_name()' 而不是 'select database()'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM