遇到问题需要使用php

时间:2017-03-03 18:44:08

标签: php mysql mariadb

我再次来到这里我遇到了一个有趣的问题,我一直在使用的PHP页面,来发现在家里我使用的是MySQL,而在学校里,我也正在研究这个项目,我正在使用MariaDB。这个问题伴随着SQL查询和创建表,MariaDB在使用时间戳时需要一个默认值,否则它会自动添加一个ON UPDATE CURRENT_TIMESTAMP,这不是我想做的事情,我设置了一个用户加入时间戳的表,在创建新用户时设置。只要你在mysql服务器上运行它就可以正常工作。但是当我正在研究它时,前几天发现学校的开发数据库是MariaDB 10.x所以用户加入日期总是在更新...查看MariaDB KB并且它声明表格时需要默认值是创建了否则ON UPDDATE会被添加......所以我想出了以下内容(跟我一起,我知道这可能看起来很糟糕,这就是为什么我只想抓住这个类型...

  function create_tables(){
// This call will intregrate to the install, this will get the version numbers of the sql server, and deturmine what the server is running and then will
//create the tabels for the correct server.
global $db;
$query = "SELECT Version() as 'dbV' ";
$stmnt = $db -> prepare($query);
$stmnt -> execute();
$result = $stmnt -> get_result();
$results = $result -> fetch_assoc();
$version = $results['dbV'];
// For MYSQL 5.6 or newer will be required
// For MariaDB 5.5, and all 10.x will be supported
$shortVersion = str_replace('.','',$version);
//version division ... Yay
if (((int)$shortVersion / 10000) >=1 ) {
  echo var_dump('MariaDB version: ' . $version );
  return create_tables_MariaDB();
}elseif (((int)$shortVersion / 5700) >=1 && ((int)$shortVersion - 5599) >=1) {
  echo var_dump('MySQL version: ' . $version );
  return create_tables_MySQL();
}elseif (((int)$shortVersion - 5599) < 1) {
  echo var_dump('MariaDB version: ' . $version );
  return create_tables_MariaDB();
}else{
  echo "<div class='alert alert-danger'>You will need to use one of the following DB servers to use this site: MySQL 5.6.X or 5.7.X, MariaDB 5.5.X or 10.X.X. PLease install or update your database server and try again.</div>";
  exit();
}
}

问题是MySQL和MariaDB都有5.5.X的有效版本所以我不得不选择......所以我的主要问题是::

是否有更简洁的方法来获取数据库服务器类型?只检查Mysql或MariaDB然后调用特定函数来创建表格会更加清晰......

任何建议都会非常有用,

Jesse Fender

这是我新编辑的,更简洁的代码,感谢@Demi的帮助......

  function create_tables(){
// This call will intregrate to the install, this will get the version numbers of the sql server, and deturmine what the server is running and then will
//create the tabels for the correct server.
global $db;
$query = "SHOW VARIABLES LIKE '%version%'";
$stmnt = $db -> prepare($query);
$stmnt -> execute();
$result = $stmnt -> get_result();
while ($results = $result -> fetch_assoc()) {
    $version[] = $results;
}

// echo var_dump($version );
$ver_name = $version[5]['Value'];
//echo var_dump(substr($ver_name,0,5));
if (substr($ver_name,0,5)==="MySQL") {
  return create_tables_MySQL();
}elseif (substr($ver_name,0,5)==="Maria") {
  return create_tables_MariaDB();
}else{
  echo "<p class='alert alert-danger'>Invalid Database Connection. Please only use MySQL or MariaDB servers.</p>";
}
}

1 个答案:

答案 0 :(得分:1)

改为使用

SHOW VARIABLES LIKE "%version%";

MariaDB上的输出

MariaDB [(none)]> SHOW VARIABLES LIKE "%version%";
+-------------------------+-----------------------------+
| Variable_name           | Value                       |
+-------------------------+-----------------------------+
| innodb_version          | 5.6.34-79.1                 |
| protocol_version        | 10                          |
| slave_type_conversions  |                             |
| version                 | 10.1.21-MariaDB             |
| version_comment         | MariaDB Server              |
| version_compile_machine | i686                        |
| version_compile_os      | Linux                       |
| version_malloc_library  | system                      |
| version_ssl_library     | OpenSSL 1.0.2k  26 Jan 2017 |
| wsrep_patch_version     | wsrep_25.16                 |
+-------------------------+-----------------------------+
10 rows in set (0.01 sec)

MySQL上的输出

mysql> SHOW VARIABLES LIKE "%version%";
+-------------------------+------------------------------+
| Variable_name           | Value                        |
+-------------------------+------------------------------+
| innodb_version          | 5.6.35                       |
| protocol_version        | 10                           |
| slave_type_conversions  |                              |
| version                 | 5.6.35                       |
| version_comment         | MySQL Community Server (GPL) |
| version_compile_machine | x86_64                       |
| version_compile_os      | Linux                        |
+-------------------------+------------------------------+

请记住,MySQL有许多不同的版本和风格

mysql> SHOW VARIABLES LIKE "%version%";
+-------------------------+--------------------------------------------------+
| Variable_name           | Value                                            |
+-------------------------+--------------------------------------------------+
| innodb_version          | 5.6.20-68.0                                      |
| protocol_version        | 10                                               |
| slave_type_conversions  |                                                  |
| version                 | 5.6.20-68.0-log                                  |
| version_comment         | Percona Server (GPL), Release 68.0, Revision 656 |
| version_compile_machine | x86_64                                           |
| version_compile_os      | Linux                                            |
+-------------------------+--------------------------------------------------+
7 rows in set (0.01 sec)