如何在PHP中比较MySQL版本?

时间:2022-02-12 04:39:12

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

好的,所以我得到了我的MySQL版本:

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

Which gives me a number like: 5.1.36

这给了我一个像: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.

这一切都很好。我需要做的是将该版本与另一个版本进行比较。当我想到version_compare()时,我正打算尝试编写我的函数来比较它们。然而,经过测试我变得不确定,但也许我只是不确定MySQL版本号如何工作。

This is what I tested:

这是我测试的:

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

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

or

5.1.36 < 5.1.4

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.

我认为这将返回true,5.1.36小于5.1.4。我的理由是,我认为5.1.4实际上是5.1.40而不是5.1.04。也许我错了。

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

所以我想错了,或者函数返回错误的结果?

4 个解决方案

#1


8  

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

功能正确。编号系统是M.m.r,其中每个“数字”是十进制数。

  • M is the major version number
  • M是主要版本号

  • m is the minor version number
  • m是次要版本号

  • r is the revision number
  • r是修订号

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)...

因此,5.1.36将是5.1次要版本的修订版36 ......因此,5.1.4将是版本4(因此36> 4)......

#3


2  

http://www.php.net/manual/en/mysqli.get-server-version.php

mysqli's get_server_version() method will return the version as an integer.

mysqli的get_server_version()方法将返回版本为整数。

main_version * 10000 + minor_version * 100 + sub_version (i.e. version 4.1.0 is 40100).

main_version * 10000 + minor_version * 100 + sub_version(即版本4.1.0是40100)。

#4


0  

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.

我认为这将返回true,5.1.36小于5.1.4。我的理由是,我认为5.1.4实际上是5.1.40而不是5.1.04。也许我错了。

Yes, 5.1.36 is greater than 5.1.4.

是的,5.1.36大于5.1.4。

#1


8  

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

功能正确。编号系统是M.m.r,其中每个“数字”是十进制数。

  • M is the major version number
  • M是主要版本号

  • m is the minor version number
  • m是次要版本号

  • r is the revision number
  • r是修订号

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)...

因此,5.1.36将是5.1次要版本的修订版36 ......因此,5.1.4将是版本4(因此36> 4)......

#2


#3


2  

http://www.php.net/manual/en/mysqli.get-server-version.php

mysqli's get_server_version() method will return the version as an integer.

mysqli的get_server_version()方法将返回版本为整数。

main_version * 10000 + minor_version * 100 + sub_version (i.e. version 4.1.0 is 40100).

main_version * 10000 + minor_version * 100 + sub_version(即版本4.1.0是40100)。

#4


0  

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.

我认为这将返回true,5.1.36小于5.1.4。我的理由是,我认为5.1.4实际上是5.1.40而不是5.1.04。也许我错了。

Yes, 5.1.36 is greater than 5.1.4.

是的,5.1.36大于5.1.4。