简体   繁体   English

如何在 PHP 中打印连接的数据库信息(例如数据库名称)?

[英]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.我正在尝试在 PHP 中执行 SQL 查询,但即使数据库已连接,它也返回 0 行,但是当我尝试在 MSSQL 中执行相同的查询时,它返回一行。

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.我需要一个等效的mysql_db_name来打印数据库名称。 This one is deprecated and does not work in php 7.这个已被弃用,在 php 7 中不起作用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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