2

PHP+MySQL, What would happen when calling mysql_connect twice, like that:

$link = mysql_connect($server, $user, $pass);
$link = mysql_connect($server, $user, $pass);

PHP doesn't seem to generate any notice. Is the second line ignored or the new connection is established (if so, does that auto-close the previous connection) ?

I know, those situations shouldn't happen in the first place.

Saix
  • 103
  • 9
  • 4
    Read about the `new_link` parameter which can be passed to `mysql_connect()`: http://www.php.net/manual/en/function.mysql-connect.php – ComFreek Dec 12 '13 at 16:08
  • Please don't use deprecated `mysql_` functions... use `mysqli_` functions or a DB wrapper instead... – Ronald Swets Dec 12 '13 at 16:08
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – h2ooooooo Dec 12 '13 at 16:09
  • 2
    @h2ooooooo: I'm running old PHP version, also these php codes are kinda old, I won't update the code just because some function got depricated years after. Wasn't my question either. I know it is depricated. – Saix Dec 12 '13 at 16:11
  • @ComFreek: Thanks, I did succesfully overlooked that before. The second line is ignored ( = same connection is returned to variable ). – Saix Dec 12 '13 at 16:14
  • a=10; a=10; what output will come? same scenario here too – Nana Partykar Sep 14 '15 at 13:35

2 Answers2

3

I know its an old question, but i was wondering the same thing myself. So i did a test to see if php would actually ignore one connection, or open a new connection (which would be bad as it would probably put some additional load on the database).

<?
$test1 = mysqli_connect("127.0.0.1","root","pass","test_db");
$test2 = mysqli_connect("127.0.0.1","root","pass","test_db");
print "<pre>";
print_r($test1);
print_r($test2);
print "</pre>";
?>

The above will output a [thread_id]. By comparing the two ids, i found that they were not similar, and therefore i must assume that 2 connections are now open.

JPJens
  • 1,185
  • 1
  • 17
  • 39
0

I was also looking for the answer. Tnx to ComFreek I got to solve my problem. I was facing the error

mysql err 2014: Commands out of sync; you can't run this command now

which is because of executing new query in middle of fetching another one in the connection. Then I was wondering if the mysqli_connect always returns a new connection or it returns an opened one if exists. About parameter 'new_link' the documentation of the deprecated "mysql_connect" says (http://php.net/manual/en/function.mysql-connect.php) :

new_link

If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored.

but the documentation of the newer class "mysqli::__construct" says (http://php.net/manual/en/mysqli.construct.php):

Opens a connection to the MySQL Server.

which means it always returns a new connection so I checked it out and did the new query (the one in middle) with a new connection and it's fixed.

navid
  • 1,022
  • 9
  • 20