There is a database on MySQL, it has a table of 24 fields, regular, MyISAM, only 2,628 rows

Through the API, from time to time, queries are made to this table via PHP / mysqli, where the usual SELECT searches for a single row for one field.

In most cases, there are two options:

  • Record found, recording is in progress and everything is in order
  • The record was not found (it is really not in the table) and was refused.

The other day, there were complaints that customers are accessing this table, and they get a result - the record was not found, I look at the log of saved queries, take the value of the search field from there, and find the record with this field. Moreover, when creating a record, time is set, and at the time of the request, this record in the table was exactly

Query to the database is very simple:

function element_GetByNumber($number) { $data = db_query("SELECT * FROM `table` WHERE `number` = '" . db_escape($number) . "' LIMIT 1;"); if ($data && $row = mysqli_fetch_assoc($data)) { return $row; } return NULL; } 

The db_query function itself is described even simpler:

 function db_query($query) { if ($GLOBALS['DB_LINK']) { return mysqli_query($GLOBALS['DB_LINK'], $query); } else if (db_connect()) { return mysqli_query($GLOBALS['DB_LINK'], $query); } return false; } 

Connection to the database in the form of db_connect() opens at the beginning of work and closes upon completion, that is, the lack of connection can be excluded. The link to the base is saved to the global variable and does not change, the base is one

With such just those there are notarized screenshots from clients and confirmation in the logs that the element that is in the table and should be found, returns "not found", that is, NULL

I just can't understand what's wrong, and why in some cases ordinary SELECT can fail.

Perhaps someone has already encountered such a thing, what could be the options, where to look, what to check, what to fix?

At the moment I have added a log by the result of the query with the output of the result itself and the last SQL error in the hope that they will clarify the situation. What else can be done to catch this byaki?

UPD: Log gave additional information. When connected, mysqli_connect returns false. It seems that the problem was just in the absence of a connection. Added a connection error to the log, waiting for the result

UPD2: I found out that mysqli_connect gives false silently, mysqli_connect_error () is empty and mysqli_connect_errno () is 0 after connection. Everything is very bad. Added a temporary crutch with 10 connections with a pause of 10ms and even more logs. There is nothing in the MySQL logs, nothing in the Apache logs either. Ghost

  • то есть отсутствие подключения можно исключить - it is impossible without checking the corresponding code. Maybe you fail to connect silently ignore the problem. And immediately: only in very rare situations the request returned an error and the request did not return data equivalent. In most cases, the request with an error should throw an exception (mysqli can do it itself, but only if asked in advance) - Minor
  • I wouldn’t like to see a screenshot with the MySQL exception instead of screenshots with "Element not found", but thanks for the advice, I will leave it as a last resort. - wirtwelt
  • No one offers you to display any information about the exception to the user. This information is needed in the logs. And the user is a normal message that something has gone very badly in the way the developer assumed. - Fine
  • Yes, I understood, already added to the log, I look - wirtwelt

1 answer 1

Haha seems to have to

 do { $res = mysqli_connect(); usleep(...); ...if (...) ... break; } while (!$res); 

As in the client on bare sockets.

  • Practically copied my crutch) Yes, already 10 hours and not a single break. It helps, but I would like to get to the bottom of the reason for such a hat, rather than plugging it with crutches - wirtwelt