0

I have a live database-driven web application that is almost entirely built on php/mysql interaction. It's been live for two and a half years and in that time the code has not changed and I've never had an issue.

About a week ago, the site just suddenly stopped pulling any data from the database. At first I thought the host was just down, it happens sometimes. But the problem persisted and my users started noticing and asking questions, so I had to investigate.

I first tried connecting directly to the database via MySQL Workbench. This was successful. This told me a few things: that the database was intact and unharmed; that there was no problem with the url/username/password/etc; and that the problem therefore must be a php issue.

Upon debugging the php, I became perplexed because the php wasn't behaving as though it couldn't connect; rather, it was behaving as if it was connecting to an empty database. It didn't throw any error message upon connecting, but then would return nothing from queries that 100% should've returned something.

Long story short, after banging my head against a wall for a while, I got to a point where I narrowed the problem down to this standard construct used to loop through a returned MySQL object:

while($row = $result->fetch_array(MYSQL_ASSOC))
  do stuff

This has always worked. I've never, ever had a problem with it. But I'm having a problem now, so, on a whim I tried changing it to

while($row = $result->fetch_assoc())
  do stuff

And just like that, it started working again. Then I tried

while($row = $result->fetch_array())
  do stuff

This also worked. Then I tried

while($row = $result->fetch_array(MYSQLI_ASSOC))
  do stuff

and that too worked.

So the problem is that for some reason, the MySQL_ASSOC parameter stopped working all of a sudden. My question is, what would cause that to happen?

My only guess is the version of PHP my hosting account is using changed on its own for some reason. I checked and it's currently using "PHP 7.2. FastCGI". Unfortunately, I don't remember what version it was using before. I can change the version if I want, all the way back to 5.6. I just don't know if I should.

Does anyone have any idea what could've caused this to happen?

namkcuR
  • 95
  • 8
  • 3
    `MYSQL_ASSOC` is a constant associated with the *very* old, and now deleted `mysql_` library. It should not even be defined in any remotely recent PHP version. [Edit: actually it's not even defined in PHP 5.](https://3v4l.org/1GGF4) – Jeto Sep 01 '20 at 07:33
  • Maybe your server did a update? – Ingus Sep 01 '20 at 07:36
  • 1
    As already stated, that constant is for the old mysql api which was completely removed in PHP 7, so if they upgraded from 5.6 to 7.2, it's correct that it broke. And no, you should _not_ revert back to PHP 5.6. That version is no longer supported and hasn't gotten any security updates in over 1,5 years. – M. Eriksson Sep 01 '20 at 07:36
  • @MagnusEriksson as it seems to make it work again he has to return back to 5.6 .. Yet not recommended but to get service back online it is probably a thing to do. .. Other thing would be to upgrade script ASAP – Ingus Sep 01 '20 at 07:39
  • 1
    IF its something you need to be up and running now, then you could define the constant yourself in a commonly included script (if any) to be = `MYSQLI_ASSOC`. Just make sure it isn't just left in there and someone is tasked to correct the code. – Nigel Ren Sep 01 '20 at 07:45
  • 1
    @Ingus - Since they seem to be using mysqli anyway, it shouldn't be much more complicated to fix than do a search & replace on `MYSQL_ASSOC` to `MYSQLI_ASSOC` in their code base. I wouldn't recommend downgrading to 5.6 in any circumstance. – M. Eriksson Sep 01 '20 at 08:33
  • @MagnusEriksson As much as i know that would not bring security. But yeah to skip downdgrade that would be a better solution! – Ingus Sep 01 '20 at 08:46

0 Answers0