tags:

views:

34

answers:

4

I am dealing with a (PHP5) class that uses a MySQL resource (public $_conn). When I do print_r($this->-conn), I get a text such as Resource id #30.

Is it possible to get the connection details for that particular resource? I have to get at least the MySQL username?

A: 

I very much doubt it is possible to get that kind of information out of a database resource.

A practice I have seen often is to store such information as additional variables at the time the connection is established.

$this->conn = mysql_connect(.......);
$this->info_username = $username;
$this->info_database = $database;

... etc ....

I would avoid storing the password this way so that it doesn't become visible in a full var dump.

Pekka
I have been trying to trace the values for user/pass, but somehow, somewhere, someone in the code is overwriting something with something else and I think that haven't been able to trace all the calls to mysql_connect and all the overwrites of the `$_conn` variable. (in other words: I need to do this for debugging)
Tom
+4  A: 

You should have the username somewhere in your files, so you can create the connection resource. But you can obtain the current user with the following SQL command:

SELECT CURRENT_USER();

The PHP Code for this:

$result = mysql_query("SELECT CURRENT_USER()",$this->_conn);
$row = mysql_fetch_array($result);
$username = $row[0];
jigfox
damn - you type faster than me!
symcbean
+3  A: 

No. That's not what the resource is intended for - its just a placeholder for the connection to the remote system (which may have been authenticated).

But if your code has connected to the database then it has already provided the username - why did it forget?

The server does need to know which connection relates to what user to determine permissions - so it is possible, once the connection is established to run a query:

SELECT CURRENT_USER();

C.

symcbean
A: 

Try to use

$link = mysql_connect (...);
mysql_stat ($link);
Alexander.Plutov