Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compare MySQL Versions in PHP?

Okay, so I'm getting my MySQL Version like so:

preg_replace('#[^0-9\.]#', '', mysql_get_server_info());

Which gives me a number like: 5.1.36

That's all good. What I need to do, is compare that version with another version. I was about to attempt to write my function to compare them, when I thought of version_compare(). However, upon testing I became unsure, but perhaps I'm just not sure how MySQL Version Numbers work.

This is what I tested:

version_compare('5.1.36', '5.1.4', '<');

or

5.1.36 < 5.1.4

I assumed that this would return true, that 5.1.36 is less than 5.1.4. My reason for that is, I figure 5.1.4 is actually 5.1.40 not 5.1.04. Perhaps I'm wrong there.

So am I thinking wrong, or is the function returning the incorrect result?

like image 691
Jason Lewis Avatar asked Jun 30 '10 17:06

Jason Lewis


1 Answers

The function is correct. The numbering system is M.m.r where each "number" is a decimal number.

  • M is the major version number
  • m is the minor version number
  • r is the revision number

So 5.1.36 would be revision 36 of the 5.1 minor version... Therefore, 5.1.4 would be revision 4 (and hence 36 > 4)...

like image 82
ircmaxell Avatar answered Sep 18 '22 15:09

ircmaxell