3

I'm having a problem my php script witch you can see here: http://codepad.org/F0qhElRC

Is not opening a new resource for each connection from a child. Already I tryed using 127.0.0.1 or my local network IP but nothing worked, also max_user_connections in mysql is set to 0 opening a connection from web gives me a different resource for the web. But in the command line it uses the same.

In mysql_connect I specified new_link to be true, I placed a __destruct in the bd class to close the connection if the child get's destroyed. Closing the connection does not make php open a new one even after I restart the script. The resource could be free and so I should count on that but in the childs I also have tryed placing a sleep but that doesn't make it run a new resource.

Any sugestions on how to debug this?

Basically I whant a new conn for every new script or child call.

Thanks in advance. Best regards,

Fernando André
  • 1,213
  • 3
  • 19
  • 32
  • How do you determine if it's a new connection? By var_dumping the resource id? It's the same if you re-start your script. – cweiske Jun 28 '11 at 13:22
  • I do a print of the resource $conn = mysql_connect(xxx); print $conn."\n"; I know it may get reused but I don't understand how the childs of the fork end up using the same, since each childs opens a different connection. I'm using php 5.3.6 – Fernando André Jun 28 '11 at 13:56
  • `print $conn` just shows you the php resource ID, not a "connection ID" whatsoever. resource IDs are re-numbered from 0 when php starts. – cweiske Jun 28 '11 at 14:34
  • But since I'm using the same script only diference is that it forks, it should return a diferent conn every time, right ? each connection will have a diferent resource ID, no ? – Fernando André Jun 28 '11 at 14:56
  • no. each fork has an own copy of the resource ID list, and it's always 0. they are not shared between forks/php processes. – cweiske Jun 28 '11 at 15:16
  • So if the parent has a resource list = resource id #1 , the childs list will be resource list = null; if for each child I open a new connection then it should be the next resource available witch would be #0 ? Here I'm getting all the childs with the same resource. – Fernando André Jun 28 '11 at 15:48
  • No. your parent PHP process does not have any resource ID given out yet. So it's at 0 at the parent, and you're getting "1" in every child process. – cweiske Jun 28 '11 at 18:05

2 Answers2

2

You've managed to omit all the relevant code, but many database extensions in PHP reuse currently open connections unless you specifically instruct them not to do so. If you are using mysql_connect(), have a look at the $new_link parameter:

mysql_connect — Open a connection to a MySQL Server
Report a bug
Description
resource mysql_connect ([ string $server = ini_get("mysql.default_host") [, string $username = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_password") [, bool $new_link = false [, int $client_flags = 0 ]]]]] )

Opens or reuses a connection to a MySQL server.

.

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.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • $this->conn = mysql_connect( $this->_server, $this->_user, $this->_pass, true, MYSQL_CLIENT_COMPRESS ); Already have that , this is my mysql_connect line. – Fernando André Jun 28 '11 at 14:48
  • http://www.phpclasses.org/package/1148-PHP-Easier-insert-update-methods-with-MySQL-.html I used this class has the base for my code witch has litle modifications. – Fernando André Jun 28 '11 at 15:07
0

Are you using sql.safe_mode (check your configuration files)? The documentation for mysql_connect indicates that *new_link* is ignored in such cases.

borrible
  • 17,120
  • 7
  • 53
  • 75