PHP stopped implicit MySQL database link discovery

On one of my servers, a strange thing happened to me.

Usually, when a script executes mysql_query($query) , it just works if mysql_connect() was successfully called in advance.

Now it works no more than half the time. Now, I suddenly had to edit all the mysql_query calls in the shopping cart to include what was previously used for an additional database link. In other words, mysql_query($query, $db_link) now looks like code.

I did this temporarily so that visitors can still use the site, but I really need to know what could have caused such an event.

Is there any parameter that can be somehow changed in one of the php.ini files that can cause this? I know that I did not touch them when this happened, and I was the only one who visited the server at that time (as far as I can tell).

+4
source share
1 answer

A call to mysql_query() without a connection parameter will use the last connection. So while you only connect to one database, you will not see any problems.

From documents :

If no link identifier is specified, the last link assumed by mysql_connect () is assumed. If no such link is found, it will try to create one as if mysql_connect () was called without arguments. If the connection is not found or not established, an error of level E_WARNING is generated.

If you add or include some new code that connects to another database, for example, perhaps a track detector or analytics tool, this may violate the existing code because it establishes a different mysql connection - and any of your queries that occur after a new connection is established will now use the wrong connection.

This will happen even if the additional code is correctly written and will always use its own connection identifier - it will not have problems with accidentally using your connection, but your code will still accidentally use its connection.

To use mysql_query() , you really need to track the database connection and use it in every query.


All of the above - if you make changes to all mysql_* calls mysql_* , go to mysqli , mysql_* functions are mysql_* and are no longer supported . The mysqli extension can be almost a replacement for replacement, and also provides support for features such as prepared statements.

The PDO extension is another possible replacement, but switching from mysql_* will require a bit more rewriting.

+2
source

Source: https://habr.com/ru/post/1493993/


All Articles