运行 问题需要数据库服务器类型使用 php

Running into issue need db server type using php

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

  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,然后调用特定函数来创建 tables...

会干净得多

任何建议都会很有帮助,

杰西·芬德

这是我新编辑的更简洁的代码,感谢@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>";
}
}

改用这个

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)