php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #14279 MySQL changes the link to localhost when not needed.
Submitted: 2001-11-29 09:01 UTC Modified: 2001-12-02 17:08 UTC
From: madleo at catho dot com dot br Assigned: zak (profile)
Status: Closed Package: Feature/Change Request
PHP Version: 4.0.6 OS: Linux-i386
Private report: No CVE-ID: None
 [2001-11-29 09:01 UTC] madleo at catho dot com dot br
We have a PHP script that suddenly changes its connection to a MySQL DB to the MySQL DB the localhost has. It wasn't suppose to do so. It happened after we created a new connection and then closed this new connection to use the old connection. The problem is that PHP now tries to connect to localhost, instead of using the only connection available. If it succeeds, it will connect to a host that could have nothing to do with the application (imagine a MySQL master-slave environment and the application is suppose to connect to the Master and, instead, it connects to the localhost where there's a slave and start updating data. This will corrupt the slave machine, which is what happened to us)

<?
$conn = mysql_connect("somehost", "user", "passwd");

echo mysql_get_host_info(), "<br>";

$conn2 = mysql_connect("anotherhost", "anotheruser", "anotherpasswd");
mysql_close($conn2);

echo mysql_get_host_info(), "<br>";

?>

Ain't it a good reason for a review in this part of the code?

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-12-02 03:06 UTC] zak@php.net
Perhaps the code should be changed so that when the 
default connection is closed, the next available 
connection is set as the default.

If no one disagrees, I will do this. However, given that 
the MySQL functions open a connection to localhost if no 
default connection is set, I am changing this from a bug 
to a feature/change request.

Two additional notes on this:

Monty Widenius from MySQL recommends explicitly passing 
the connection handle to mysql_query() -- this helps avoid 
issues like this.

Either change your mysql db auth settings or set your 
default MySQL host in your php.ini file to an invalid 
host. This will prevent you from accidentally connecting 
to a database that you do not wish to connect to.

 [2001-12-02 17:08 UTC] zak@php.net
I chatted about this idea with Yasuo Ohgaki - he pointed 
out a few potential issues with the change. While having 
the MySQL extension automatically set a new default 
connection might be more convenient, it would likely 
introduce another set of confusing behaviors to the 
function.

Currently the behavior of many of the MySQL functions is:
Accept an optional connection argument. If no connection 
argument is given, the last connection opened is used by 
default. If no connection is open, the function attempts 
to connect to a MySQL database by calling mysql_connect() 
without arguments. 

We probably should not make this any more complex. The 
current behavior has been in place for a long time - long 
enough that we should not break backwards compatibility.

I hope that my earlier suggestions on a solution serve you 
well for your problem.

I am closing this request.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 23:01:27 2024 UTC