-1

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.

ADyson
  • 57,178
  • 14
  • 51
  • 63
showtime
  • 1
  • 1
  • 17
  • 48
  • If you get boolean `false` it indicates the query failed in some way. Enable error checking in PDO to find out why. See https://www.php.net/manual/en/pdo.error-handling.php for details – ADyson Oct 19 '21 at 12:59
  • But why do you need to ask mysql this? Surely your PHP code was the one which told mysql which database to select when the connection was opened? So really your PHP code should already know this information without needing to ask the mysql server? – ADyson Oct 19 '21 at 13:00
  • @ADyson I just want to actually be able to print the name and other info for the connected db. This way I make sure that I am actually connected to the right one. – showtime Oct 19 '21 at 13:02
  • 1
    But like I said, you will have passed that info to mysql when you created the PDO connection, so you don't need to ask again necessarily... you could just retrieve that info from the connection details. – ADyson Oct 19 '21 at 13:04
  • Anyway if you do want to ask mysql for this information, `select database()` should be the correct command to run, so if you're getting an error from that you need to find out why - see back to my first comment – ADyson Oct 19 '21 at 13:04
  • @ADyson I tried with errror handling as u suggested: PHP Fatal error: Uncaught PDOException: SQLSTATE[42S02]: Base table or view not found: 208 [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid object name 'users'. – showtime Oct 19 '21 at 13:05
  • `Invalid object name 'users'` looks like it would come from the second query you showed, not the `select database` command – ADyson Oct 19 '21 at 13:06
  • Hang on...looking at that error, are you connecting to MS SQL Server, not MySQL? It was confusing because you mentioned mysql in the question. You realise they are different, I hope?? – ADyson Oct 19 '21 at 13:06
  • 1
    If you want to do this in SQL Server then use `select db_name()` (as per https://stackoverflow.com/questions/39734735/find-out-current-database-name and 100 others) – ADyson Oct 19 '21 at 13:08
  • @ADyson sorry, I am using ms sql server indeed. But the error message seems weird, I dont get it. – showtime Oct 19 '21 at 13:09
  • `the error message seems weird,`...like I said, the error message you showed seems to be related to your `select * from users` query, not the `select database()` one. And it just means your selected database doesn't have a table or view called "users". – ADyson Oct 19 '21 at 15:10
  • So did you try `select db_name()` yet? – ADyson Oct 19 '21 at 15:10
  • mssql is not mysql – Tuckbros Oct 19 '21 at 16:01

1 Answers1

1

Try 'Select db_name()' instead of 'select database()'

GabrielVa
  • 2,353
  • 9
  • 37
  • 59