Reputation: 4077
I am implementing a script on a site where they don't want to give me access to their main database, so they made a second database for me. However, after calling the function that connects to my database from index.php (where they have a connection to their database), it looks like their connection is lost. Is this normal? Is it possible to establish a second connection using mysql_connect() without interfering with the first connection? I never use mysql_close by the way.
This is my connect function
public function connect() {
$link = mysql_connect('localhost', 'user', 'pwd');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('database');
return $link;
}
Thanks!
Upvotes: 1
Views: 163
Reputation: 50592
PHP's mysql_
family of functions all take an optional argument link
, where you specify which link is to be used for the given function. Check out the docs here for mysql_error
(as an example): http://www.php.net/manual/en/function.mysql-error.php
The trouble is, many new developers who are calling mysql_connect
directly in their code rather than using a wrapping database class (or, even better, PDO) will tend to not supply that optional argument. What happens when you do not give the link
argument is that the function will assume you want to use the link that was most recently created - in this case, your link.
You can correct this omission in your own function:
public function connect() {
$link = mysql_connect('localhost', 'user', 'pwd');
if (!$link) {
die('Could not connect: ' . mysql_error($link));
}
mysql_select_db('database', $link);
return $link;
}
.. but that doesn't help out the rest of the project, where the developers are probably calling mysql_query
all over the place without passing the link, so it will default to your newer link. Best way around this without having to fix all the other code is to use PDO for your additional database access.
Another thing that could be affecting the code, even if the other developers are passing the link
argument, is that you aren't using the new_link
argument when you call mysql_connect
. You're basically "stealing" their link! If they were careful to always use the link
argument for mysql_query
, you can use the new_link
argument to make sure you don't hijack their database connection:
public function connect() {
$link = mysql_connect('localhost', 'user', 'pwd', true);
if (!$link) {
die('Could not connect: ' . mysql_error($link));
}
mysql_select_db('database', $link);
return $link;
}
Upvotes: 2
Reputation: 3066
One way I've gotten around this in the past is to use mysqli for the second database connection to avoid conflicts. It's evil and hackish, but it was the shortest way around the problem:
http://php.net/manual/en/book.mysqli.php
Upvotes: 0
Reputation: 5713
Check out the post by sedativchunk. He has created a PHP class that allows you to have multiple connections open. However, I have not used it myself.
Upvotes: 0
Reputation: 63452
Sure, just make sure you use that $link
variable you created, such as in the mysql_select_db()
call, the mysql_error()
call and wherever else you use the mysql_*
functions.
Upvotes: 1