I have 2 database connections in this program, they are:
$dbcn_anx = @mysql_connect(‘server1.x1.com’, ‘root’, ‘x2009);
if (!$dbcn_anx) {
exit (’<p> Unable to connect to X1 Database at this time.');
}
$dbcnx_nd = @mysql_connect(‘localhost’, ‘root’, ‘xx9009’);
if (!$dbcnx_nd) {
exit (‘<p> Unable to connect to the LocalHost Database’);
}
These connections are made OK.
But when I go to execute a MySQL command such as:
$sql_chk_ip = "SELECT id FROM dd.Block_IP WHERE '$ip' BETWEEN start_ip AND end_ip LIMIT 1";
$query_chk_ip = mysql_query($sql_chk_ip, $dbcnx_nd) or die(mysql_error());
THEN I get this Error message:
Notice: Undefined variable: dbcnx_nd in /var/www/html/dreamdates.com/new2/index.php on line 14
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /var/www/html/dreamdates.com/new2/index.php on line 14
I’d suggest losing the @ so you can actually see any errors. A catastrophic failure of the operation could return null instead of false – just another reason I’ve never been wild about the mySQL_ functions in the first place and bailed for PDO at the first opportunity.
“Undefined variable”, when you know you have defined the variable as in this case, probably points to the fact that the calling script cannot find it because of the way you have included the file containing the dependency.
To prove/disprove the above case temporarily hardcode the missing variable above your query.
I did what you suggested.
That is removed the @ from behind MySQL db connection.
And actually added language to show that DB connection is successful.
And all I get is response that the DB connections were successful.
Here:
Connected to dbcnx_ax OK
Connected to LocalHost dbcnx_nd OK
Notice: Undefined variable: dbcnx_nd in /var/www/html/dreamdates.com/new2/index.php on line 14
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /var/www/html/dreamdates.com/new2/index.php on line 14
This is really amazing
Really an unacceptable error message.
I mean the DB connection to $dbcnx_nd is made OK, yet it is saying that this Variable is undefined!
Really amazing
I did as you suggested.
That is I moved the DB connection code from the include file into the page that is making use of this DB connection, and yet it still gives same Error message!
Here:
Connected to $dbcn_anx DB OK
Connected to LocalHost $dbcnx_nd OK
Notice: Undefined variable: dbcnx_nd in /var/www/html/dreamdates.com/new2/index.php on line 29
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /var/www/html/dreamdates.com/new2/index.php on line 29
After I remove the 1st DB connection, to the outside server, then Error message is gone and the Php code is able to connect to the local DB.
But why is this?
To be exact:
1- I am, I need to, connect to the outside server MySQL Db and to the localhost MySQL DB, which I do via these codes:
$dbcn_anx = mysql_connect(‘server1.anx.com’, ‘root’, ‘x90009’);
if (!$dbcn_anx) {
exit (‘<p> Unable to connect to AnX Database.’);
} else {
echo ‘Connected to AnX DB OK’;
}
$dbcnx_nd = mysql_connect(‘localhost’, ‘root’, ‘y90009’);
if (!$dbcnx_nd) {
exit (‘<p> Unable to connect to the NetDIVE Database’);
} else {
echo ‘<p>Connected to LocalHost OK’;
}
2- So when the above 2 DB connections exist, then this code gives the Error message that I have been posting:
$query_chk_ip = mysql_query($sql_chk_ip, $dbcnx_nd) or die(mysql_error());
3- But after I remove the $dbcn_anx DB connection and use this code:
$query_chk_ip = mysql_query($sql_chk_ip) or die(mysql_error());
Then the localhost DB connection is made OK.
What is going on?
How else are we supposed to connect to multiple different MySQL DBs from the same Php code but via like:
$query_chk_ip = mysql_query($sql_chk_ip, $dbcnx_nd) or die(mysql_error());
Actually not a “Silly question” at all. You were bang on.
That is the problem exactly was that the function was not being passed
the value of the DB connection as a variable.
BUT, and here is the BIG BUT, I always thought that a MySQL DB connection was a global variable by default. I guess I learn different now