Discussion:
Getting database name from link identifier
Larry Martell
2014-02-25 21:28:18 UTC
Permalink
Is there any way to find out what database I am connected to from a
link identifier?

I have some code this does this:

@ $db = mysql_pconnect($dbserver, $dbuser, $dbpass);
@ mysql_select_db($dbname, $db);

I know at the time this is executed $dbname is what I want it to be.
But later on when I pull data using $db, I get data from a different
database. If I try to echo $db at the time of the query I get
"Resource id #5". Can I get the database name from $db?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Lucas
2014-02-25 21:41:11 UTC
Permalink
Post by Larry Martell
Is there any way to find out what database I am connected to from a
link identifier?
@ $db = mysql_pconnect($dbserver, $dbuser, $dbpass);
@ mysql_select_db($dbname, $db);
I know at the time this is executed $dbname is what I want it to be.
But later on when I pull data using $db, I get data from a different
database. If I try to echo $db at the time of the query I get
"Resource id #5". Can I get the database name from $db?
Execute the query "SHOW DATABASE();" You should get one row back that
contains the name of the database you are currently "using".
--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Larry Martell
2014-02-25 21:46:56 UTC
Permalink
Post by Jim Lucas
Post by Larry Martell
Is there any way to find out what database I am connected to from a
link identifier?
@ $db = mysql_pconnect($dbserver, $dbuser, $dbpass);
@ mysql_select_db($dbname, $db);
I know at the time this is executed $dbname is what I want it to be.
But later on when I pull data using $db, I get data from a different
database. If I try to echo $db at the time of the query I get
"Resource id #5". Can I get the database name from $db?
Execute the query "SHOW DATABASE();" You should get one row back that
contains the name of the database you are currently "using".
I get a sql syntax error on that.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
2014-02-25 21:41:51 UTC
Permalink
Post by Larry Martell
Is there any way to find out what database I am connected to from a
link identifier?
@ $db = mysql_pconnect($dbserver, $dbuser, $dbpass);
@ mysql_select_db($dbname, $db);
I know at the time this is executed $dbname is what I want it to be.
But later on when I pull data using $db, I get data from a different
database. If I try to echo $db at the time of the query I get
"Resource id #5". Can I get the database name from $db?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
You won't be able to, you will need to execute a query on that DB
connection handler to check the DB name currently used:

SELECT DATABASE() as DB_NAME


Aziz
Larry Martell
2014-02-25 21:47:59 UTC
Permalink
Post by Larry Martell
Is there any way to find out what database I am connected to from a
link identifier?
@ $db = mysql_pconnect($dbserver, $dbuser, $dbpass);
@ mysql_select_db($dbname, $db);
I know at the time this is executed $dbname is what I want it to be.
But later on when I pull data using $db, I get data from a different
database. If I try to echo $db at the time of the query I get
"Resource id #5". Can I get the database name from $db?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
You won't be able to, you will need to execute a query on that DB connection
SELECT DATABASE() as DB_NAME
Thanks! Indeed I am not 'using' the db that $dbname is set to when I
execute the mysql_select_db command. Now I just have to figure out why
that is.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
2014-02-25 21:53:38 UTC
Permalink
Post by Aziz Saleh
Post by Aziz Saleh
Post by Larry Martell
Is there any way to find out what database I am connected to from a
link identifier?
@ $db = mysql_pconnect($dbserver, $dbuser, $dbpass);
@ mysql_select_db($dbname, $db);
I know at the time this is executed $dbname is what I want it to be.
But later on when I pull data using $db, I get data from a different
database. If I try to echo $db at the time of the query I get
"Resource id #5". Can I get the database name from $db?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
You won't be able to, you will need to execute a query on that DB
connection
Post by Aziz Saleh
SELECT DATABASE() as DB_NAME
Thanks! Indeed I am not 'using' the db that $dbname is set to when I
execute the mysql_select_db command. Now I just have to figure out why
that is.
Are you making a call to mysql_select_db besides the above during your code
execution (even on a different DB handler)? If yes, then that would be the
issue since.
Larry Martell
2014-02-25 21:59:59 UTC
Permalink
Post by Aziz Saleh
Post by Larry Martell
Post by Larry Martell
Is there any way to find out what database I am connected to from a
link identifier?
@ $db = mysql_pconnect($dbserver, $dbuser, $dbpass);
@ mysql_select_db($dbname, $db);
I know at the time this is executed $dbname is what I want it to be.
But later on when I pull data using $db, I get data from a different
database. If I try to echo $db at the time of the query I get
"Resource id #5". Can I get the database name from $db?
You won't be able to, you will need to execute a query on that DB connection
SELECT DATABASE() as DB_NAME
Thanks! Indeed I am not 'using' the db that $dbname is set to when I
execute the mysql_select_db command. Now I just have to figure out why
that is.
Are you making a call to mysql_select_db besides the above during your code
execution (even on a different DB handler)? If yes, then that would be the
issue since.
I was simplifying the code for purposes of asking the question.
Actually the app connects to 23 different databases on different
servers. The link identifiers are stored in an array. Each
mysql_select_db is called with a different link identifiers. I am
echoing out the link identifiers and the array index when I create
them and also when I use them. I see that the index is the same, the
resource id # is the same, but I'm connected to a different db then
the one I called mysql_select_db with.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
2014-02-25 22:09:58 UTC
Permalink
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
On Tue, Feb 25, 2014 at 4:28 PM, Larry Martell <
Post by Larry Martell
Is there any way to find out what database I am connected to from a
link identifier?
@ $db = mysql_pconnect($dbserver, $dbuser, $dbpass);
@ mysql_select_db($dbname, $db);
I know at the time this is executed $dbname is what I want it to be.
But later on when I pull data using $db, I get data from a different
database. If I try to echo $db at the time of the query I get
"Resource id #5". Can I get the database name from $db?
You won't be able to, you will need to execute a query on that DB connection
SELECT DATABASE() as DB_NAME
Thanks! Indeed I am not 'using' the db that $dbname is set to when I
execute the mysql_select_db command. Now I just have to figure out why
that is.
Are you making a call to mysql_select_db besides the above during your
code
Post by Aziz Saleh
execution (even on a different DB handler)? If yes, then that would be
the
Post by Aziz Saleh
issue since.
I was simplifying the code for purposes of asking the question.
Actually the app connects to 23 different databases on different
servers. The link identifiers are stored in an array. Each
mysql_select_db is called with a different link identifiers. I am
echoing out the link identifiers and the array index when I create
them and also when I use them. I see that the index is the same, the
resource id # is the same, but I'm connected to a different db then
the one I called mysql_select_db with.
Is it safe to assume that that all of those 23 connections have different
host/user/password combination?

I ran into issues before, where changing the database using the
mysql_select_db function changed the db for the connection for all users
who are sharing that connection - PHP was running as apache module and not
cgi. I ended up using mysql_connect with the third param as true to force a
new connection.

Aziz
Larry Martell
2014-02-25 22:15:07 UTC
Permalink
Post by Aziz Saleh
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
On Tue, Feb 25, 2014 at 4:28 PM, Larry Martell
Post by Larry Martell
Is there any way to find out what database I am connected to from a
link identifier?
@ $db = mysql_pconnect($dbserver, $dbuser, $dbpass);
@ mysql_select_db($dbname, $db);
I know at the time this is executed $dbname is what I want it to be.
But later on when I pull data using $db, I get data from a different
database. If I try to echo $db at the time of the query I get
"Resource id #5". Can I get the database name from $db?
You won't be able to, you will need to execute a query on that DB connection
SELECT DATABASE() as DB_NAME
Thanks! Indeed I am not 'using' the db that $dbname is set to when I
execute the mysql_select_db command. Now I just have to figure out why
that is.
Are you making a call to mysql_select_db besides the above during your code
execution (even on a different DB handler)? If yes, then that would be the
issue since.
I was simplifying the code for purposes of asking the question.
Actually the app connects to 23 different databases on different
servers. The link identifiers are stored in an array. Each
mysql_select_db is called with a different link identifiers. I am
echoing out the link identifiers and the array index when I create
them and also when I use them. I see that the index is the same, the
resource id # is the same, but I'm connected to a different db then
the one I called mysql_select_db with.
Is it safe to assume that that all of those 23 connections have different
host/user/password combination?
No, there are multiple connections on each server.
Post by Aziz Saleh
I ran into issues before, where changing the database using the
mysql_select_db function changed the db for the connection for all users who
are sharing that connection - PHP was running as apache module and not cgi.
I ended up using mysql_connect with the third param as true to force a new
connection.
I'm using mysql_pconnect, which does not have the new_link param.
Sounds like you've hit the problem I'm having. I'll try changing to
mysql_connect with new_link true and see if that fixes it.

Thanks!
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Larry Martell
2014-02-25 22:21:12 UTC
Permalink
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
On Tue, Feb 25, 2014 at 4:28 PM, Larry Martell
Post by Larry Martell
Is there any way to find out what database I am connected to from a
link identifier?
@ $db = mysql_pconnect($dbserver, $dbuser, $dbpass);
@ mysql_select_db($dbname, $db);
I know at the time this is executed $dbname is what I want it to be.
But later on when I pull data using $db, I get data from a different
database. If I try to echo $db at the time of the query I get
"Resource id #5". Can I get the database name from $db?
You won't be able to, you will need to execute a query on that DB connection
SELECT DATABASE() as DB_NAME
Thanks! Indeed I am not 'using' the db that $dbname is set to when I
execute the mysql_select_db command. Now I just have to figure out why
that is.
Are you making a call to mysql_select_db besides the above during your code
execution (even on a different DB handler)? If yes, then that would be the
issue since.
I was simplifying the code for purposes of asking the question.
Actually the app connects to 23 different databases on different
servers. The link identifiers are stored in an array. Each
mysql_select_db is called with a different link identifiers. I am
echoing out the link identifiers and the array index when I create
them and also when I use them. I see that the index is the same, the
resource id # is the same, but I'm connected to a different db then
the one I called mysql_select_db with.
Is it safe to assume that that all of those 23 connections have different
host/user/password combination?
No, there are multiple connections on each server.
Post by Aziz Saleh
I ran into issues before, where changing the database using the
mysql_select_db function changed the db for the connection for all users who
are sharing that connection - PHP was running as apache module and not cgi.
I ended up using mysql_connect with the third param as true to force a new
connection.
I'm using mysql_pconnect, which does not have the new_link param.
Sounds like you've hit the problem I'm having. I'll try changing to
mysql_connect with new_link true and see if that fixes it.
That indeed was it. I changed to mysql_connect with new_link true and
the issue went away. Thanks very much!
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
2014-02-25 22:22:15 UTC
Permalink
Post by Larry Martell
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
On Tue, Feb 25, 2014 at 4:47 PM, Larry Martell <
Post by Larry Martell
On Tue, Feb 25, 2014 at 4:28 PM, Larry Martell
Post by Larry Martell
Is there any way to find out what database I am connected to
from a
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
Post by Larry Martell
Post by Larry Martell
link identifier?
@ $db = mysql_pconnect($dbserver, $dbuser, $dbpass);
@ mysql_select_db($dbname, $db);
I know at the time this is executed $dbname is what I want it to
be.
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
Post by Larry Martell
Post by Larry Martell
But later on when I pull data using $db, I get data from a
different
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
Post by Larry Martell
Post by Larry Martell
database. If I try to echo $db at the time of the query I get
"Resource id #5". Can I get the database name from $db?
You won't be able to, you will need to execute a query on that DB
connection
SELECT DATABASE() as DB_NAME
Thanks! Indeed I am not 'using' the db that $dbname is set to when I
execute the mysql_select_db command. Now I just have to figure out
why
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
Post by Larry Martell
that is.
Are you making a call to mysql_select_db besides the above during
your
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
code
execution (even on a different DB handler)? If yes, then that would
be
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
the
issue since.
I was simplifying the code for purposes of asking the question.
Actually the app connects to 23 different databases on different
servers. The link identifiers are stored in an array. Each
mysql_select_db is called with a different link identifiers. I am
echoing out the link identifiers and the array index when I create
them and also when I use them. I see that the index is the same, the
resource id # is the same, but I'm connected to a different db then
the one I called mysql_select_db with.
Is it safe to assume that that all of those 23 connections have
different
Post by Larry Martell
Post by Aziz Saleh
host/user/password combination?
No, there are multiple connections on each server.
Post by Aziz Saleh
I ran into issues before, where changing the database using the
mysql_select_db function changed the db for the connection for all
users who
Post by Larry Martell
Post by Aziz Saleh
are sharing that connection - PHP was running as apache module and not
cgi.
Post by Larry Martell
Post by Aziz Saleh
I ended up using mysql_connect with the third param as true to force a
new
Post by Larry Martell
Post by Aziz Saleh
connection.
I'm using mysql_pconnect, which does not have the new_link param.
Sounds like you've hit the problem I'm having. I'll try changing to
mysql_connect with new_link true and see if that fixes it.
That indeed was it. I changed to mysql_connect with new_link true and
the issue went away. Thanks very much!
You are welcome. Glad it worked out.
Curtis Maurand
2014-02-26 01:17:15 UTC
Permalink
Post by Aziz Saleh
Post by Larry Martell
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
On Tue, Feb 25, 2014 at 4:47 PM, Larry Martell <
Post by Larry Martell
On Tue, Feb 25, 2014 at 4:28 PM, Larry Martell
Post by Larry Martell
Is there any way to find out what database I am connected to
from a
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
Post by Larry Martell
Post by Larry Martell
link identifier?
@ $db = mysql_pconnect($dbserver, $dbuser, $dbpass);
@ mysql_select_db($dbname, $db);
I know at the time this is executed $dbname is what I want it to
be.
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
Post by Larry Martell
Post by Larry Martell
But later on when I pull data using $db, I get data from a
different
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
Post by Larry Martell
Post by Larry Martell
database. If I try to echo $db at the time of the query I get
"Resource id #5". Can I get the database name from $db?
You won't be able to, you will need to execute a query on that DB connection
SELECT DATABASE() as DB_NAME
Thanks! Indeed I am not 'using' the db that $dbname is set to when I
execute the mysql_select_db command. Now I just have to figure out
why
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
Post by Larry Martell
that is.
Are you making a call to mysql_select_db besides the above during
your
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
code
execution (even on a different DB handler)? If yes, then that would
be
Post by Larry Martell
Post by Aziz Saleh
Post by Larry Martell
the
issue since.
I was simplifying the code for purposes of asking the question.
Actually the app connects to 23 different databases on different
servers. The link identifiers are stored in an array. Each
mysql_select_db is called with a different link identifiers. I am
echoing out the link identifiers and the array index when I create
them and also when I use them. I see that the index is the same, the
resource id # is the same, but I'm connected to a different db then
the one I called mysql_select_db with.
Is it safe to assume that that all of those 23 connections have
different
Post by Larry Martell
Post by Aziz Saleh
host/user/password combination?
No, there are multiple connections on each server.
Post by Aziz Saleh
I ran into issues before, where changing the database using the
mysql_select_db function changed the db for the connection for all
users who
Post by Larry Martell
Post by Aziz Saleh
are sharing that connection - PHP was running as apache module and not
cgi.
Post by Larry Martell
Post by Aziz Saleh
I ended up using mysql_connect with the third param as true to force a
new
Post by Larry Martell
Post by Aziz Saleh
connection.
I'm using mysql_pconnect, which does not have the new_link param.
Sounds like you've hit the problem I'm having. I'll try changing to
mysql_connect with new_link true and see if that fixes it.
That indeed was it. I changed to mysql_connect with new_link true and
the issue went away. Thanks very much!
You are welcome. Glad it worked out.
Also there is this:

|<?php
function mysql_current_db() {
$r= mysql_query("SELECT DATABASE()") or die(mysql_error());
return mysql_result($r,0);
}
?>|

Continue reading on narkive:
Loading...