FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Why can't I do this!?





Diablosblizz
Okay so I'm making a script that requires two different databases to be queried / inserted into. Both are started in the same file: config.php. Here's the connections:

Code:
$billing_connection = mysql_connect("localhost", "root", "12345");
$billing_select = mysql_select_db("atb_billing", $billing_connection);

$mybb_connection = mysql_connect("localhost", "root2", "12345");
$mybb_select = mysql_select_db("blah", $mybb_connection);


That works fine, no errors. The issue comes when I attempt to query one of the databases within the config.php file, as seen here:

Code:
$select = mysql_query("SELECT * FROM `waiting_accounts` WHERE `adminver` = '0'", $billing_connection) or die(mysql_error());


It comes back throwing this error:

Quote:
Notice: Undefined variable: billing_connection in /var/www/html/functions.php on line 7


I have no idea why this doesn't work in the same file, although if I were to include the "config.php" and do the same thing it works perfectly fine with both databases. I've also tried to have a "functions.php" file that does all the MySQL functions that would be required in config.php, but alas I still get that error. The config.php file is include as I can get variables from it with no problem.

Also, upon further investigation I've noticed that if I ignore the $billing_connection resource link then it defaults to the $mybb_connection database. I figure this is because of it being created last of the two.

Please help me here, I have no idea why it doesn't like being called in the same page. Thanks.
Nemesis234
Quote:
Problem with connecting to multiple databases within the same server is that every time you do:
mysql_connect(host, username, passwd);
it will reuse 'Resource id' for every connection, which means you will end with only one connection reference to avoid that do:
mysql_connect(host, username, passwd, true);
keeps all connections separate.


maybe worth a try?
jstei
Please use the same connection. mysql_connect knows it's using $billing_connection. You should GRANT root the required rights.
Diablosblizz
jstei, have you ever tried to use the same connection with two databases?

Nemesis234, that would have made sense but it didn't work. It ended up being the variable scope, because the code is in a function it didn't have access to the outside variable thus in it failing. Adding global $billing_connection makes it work!

Thanks for your idea though.
jmraker
You should make sure you have the $billing_connection spelled the same everywhere, the error message suggests the variable name does not exist. It might not be a database problem. If I had the problem I would try to find out where the variable goes away
Code:

$billing_connection = mysql_connect("localhost", "root", "12345");
$billing_select = mysql_select_db("atb_billing", $billing_connection);
var_dump($billing_connection);

$mybb_connection = mysql_connect("localhost", "root2", "12345");
var_dump($billing_connection);
$mybb_select = mysql_select_db("blah", $mybb_connection);
sonam
jmraker wrote:
You should make sure you have the $billing_connection spelled the same everywhere, the error message suggests the variable name does not exist. It might not be a database problem. If I had the problem I would try to find out where the variable goes away


Right, the variable don't exist. If you are using some form where you need first to define variable with posting, then you can try to put @ in a front of variable. This will stop to produce error.

Code:
@$billing_connection


Sonam
Fire Boar
Diablosblizz wrote:
It ended up being the variable scope, because the code is in a function it didn't have access to the outside variable thus in it failing. Adding global $billing_connection makes it work!


The problem has been solved, no need to continue suggesting possible fixes, sonam and jmraker.
rickylau
Fire Boar wrote:

The problem has been solved, no need to continue suggesting possible fixes, sonam and jmraker.
No way for further discussions?

Does sonam's way solve the problem actually? Isn't it avoids the message to shown for visitors who know nothing at all? It is not a good idea to find what's wrong with the script.

I would agree with the way jmraker would do, but I just wonder what would be produced by var_dump a resource link, it should be a number? Anyway if it does show nothing it should mean something wrong.

And for Nemesis' advice, I've checked with PHP manual, it seems to say that the resource will be reused when the arguments (should be all host name, user name and password?) are the same between two calls, otherwise it is not necessary to give true as the forth parameter.

If it is issue of function variable scope, would it be better to use reference variable or return the resource link, rather than global variable?
sonam
Quote:
Does sonam's way solve the problem actually? Isn't it avoids the message to shown for visitors who know nothing at all? It is not a good idea to find what's wrong with the script.


Maybe my way not solve problem but this is not way for hiding something wrong in the script, too. BTW "Notice:..." isn't some very important warning. In some situations is better to supress this notice with one @ then write "if this yes or if this no", especialy if you have some type of loop. I find @ in lot of posts "How to solve...." and I like to use it because it is faster then if function. Of course, someone other can build his/her scripts in own way.

Sonam
Fire Boar
Connection variables I would tend to make global, or else use a factory class. You don't really be wanting to pass your connection handlers all over the place.

As for "Notice" not being very important... it usually means you're doing something wrong, and indeed, in this case it does. Putting @ will simply ignore the error, causing the default connection to be used, which is exactly what the OP did not want.

Personally I would not use @ unless an expected behaviour is for the function to fail. It's pretty bad form to use it purely to suppress errors that could be avoided anyway.
sonam
Quote:
Personally I would not use @ unless an expected behaviour is for the function to fail. It's pretty bad form to use it purely to suppress errors that could be avoided anyway


I am agree with you. This is not good way to stop all notice errors but in some casess is good solution. Cool

Sonam
5aia
In my own experience I lernt that you should use only one connection at a time...
Nemesis234
5aia wrote:
In my own experience I lernt that you should use only one connection at a time...

why?
Fire Boar
5aia wrote:
In my own experience I lernt that you should use only one connection at a time...


To just one database, maybe. But when using multiple databases, multiple connections are required.
Related topics
Why our life is like that...
why not install ZendOptimizer?
Why not Adding .... this ...
why no hosting under .com
why I cannot use kf97.uni.cc domain admin help me!!
Why use IE?
GTA: San Andreas PC (OFFICIAL THREAD)
That's Why
why
Why PHPbb? '?
Why are no admins helping me?
WHY!
Why do students fail in exams...?
Why Was My Account Suspended???
Why A Beer is Better than a Woman
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.