php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #16495 return last selected databasename
Submitted: 2002-04-08 11:37 UTC Modified: 2002-07-24 04:58 UTC
From: pgl at instinct dot org Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 4.2.0 OS: all
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: pgl at instinct dot org
New email:
PHP Version: OS:

 

 [2002-04-08 11:37 UTC] pgl at instinct dot org
Would it be possible for mysql_select_db() to return the previously selected db on success, and false (as it does already) on failure? That would make it more useful for times when you want to use another db temporarily, eg:

function auth() {
    // choose the auth db
    $old_db_name = mysql_select_db('authdb');

    // ... code to authenticate a user here ...

    // change back to whatever was being used before
    mysql_select_db($old_db_name);
    }

I can't see how adding this would break any existing scripts, either, as the returned value would still have to be true (you can't create databases called '' in mysql, can you?).

regards,

Peter Lowe.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-04-08 11:45 UTC] mfischer@php.net
Well, I can see.

If someone wrote

if (true === mysql_select_db( ...

it's broken (and the API has been long this way).
 [2002-04-08 11:55 UTC] pgl at instinct dot org
True, but given that currently the only possible values the function can return are TRUE or FALSE, it seems unlikely that anyone would actually do that. Is there any other way to get the name of the current db inside a function without using global?
 [2002-04-26 09:27 UTC] pgl at instinct dot org
Would it be possible to add a flag to the end of the function call to specify that you want it to return the name of the previously selected db? eg:

$old_db_name = mysql_select_db('somedb', RETURN_OLD_DBNAME);

?
 [2002-04-26 09:31 UTC] mfischer@php.net
I think you can't accurately implement the tracking of which database you're currently using. As long as you are using mysql_select_db() it's possible to track it with the current mysql resource context. But if you issue a mysql_query() like "use another_db" you're getting out of sync.
 [2002-04-26 09:44 UTC] pgl at instinct dot org
How about a function that would give you access to the information output from the "\s" command? mysql_get_server_info, _client_info(), and _host_info() don't display this. And if this function could be implemented, might it be possible for PHP to call it before selecting to a new db (but only if RETURN_OLD_DBNAME was specified when calling mysql_select_db())?
 [2002-04-26 10:40 UTC] pgl at instinct dot org
Ah, I just found out about mysql_stat(), which seems to show most of the output from a "\s" on the mysql command line, but not the current database. Could the current db be added to the output of mysql_stat()?
 [2002-04-28 12:02 UTC] sniper@php.net
There is now 3rd optional parameter added to 
mysql_select_db() in CVS HEAD.

This:

$prev_dbname = mysql_select_db("new_db", $db, TRUE);

returns the previous database name provided that you
have selected some database before. (And not used "USE some_db" query to do that as was pointed by Markus in previous comments.

--Jani

 [2002-07-21 17:46 UTC] georg@php.net
As a result of some inconsistencies this feature was removed.

To determine the current databasename, use the SQL Command "SELECT DATABASE()", which returns the current databasename (or an error, if something happened :)
 [2002-07-24 03:59 UTC] pgl at instinct dot org
I asked for this feature request because I thought it would be useful. Is it not now considered potentially useful? Can't the "internal inconsistencies" be fixed (at some point in the future)? It's not exactly high priority, but it's still a feature request, no? :)

Thanks for the tip about "select database()". (Incidentally, couldn't PHP just send that query to get mysql_select_db() working as requested..? :))
 [2002-07-24 04:58 UTC] georg@php.net
Of course, it would be possible to implement a "SELECT DATABASE" query inside the mysql_select_db function.

But why should we implement additional client features, which already exist at the server side. In the manual there are a some functions, which are marked as deprecated (and will be removed in the near future). The major reason for that is, that there already an equivalent in the MySQL SQL-Syntax exists (e.g mysql_drop_db). Except for the transaction rollback for non committed transactions in persistent connections, the mysql extension don't uses any SQL-Syntax features.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Wed Jul 06 08:05:45 2022 UTC