PHP Mysql 不区分大小写的表选择

标签 php mysql

我正在尝试检测数据库中的表是否已经存在。但是,我编写的以下代码似乎区分大小写。如果表名是 Test 并且 $name = TEST。下面的代码不会是真的。有什么办法可以使这种情况变得不敏感吗?还是有更有效的方法?

if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '$name'")) == 1) {
    ....
}

最佳答案

确保检查lower_case_table_names 系统变量的值。默认值是根据您的操作系统分配的:

  • 0 - UNIX/Linux

  • 1 - window

  • 2 - 苹果机

根据MySQL docs :

  • 0 - Table and database names are stored on disk using the lettercase specified in the CREATE TABLE or CREATE DATABASE statement. Name comparisons are case sensitive. You should not set this variable to 0 if you are running MySQL on a system that has case-insensitive file names (such as Windows or Mac OS X). If you force this variable to 0 with --lower-case-table-names=0 on a case-insensitive file system and access MyISAM tablenames using different lettercases, index corruption may result.

  • 1 - Table names are stored in lowercase on disk and name comparisons are not case sensitive. MySQL converts all table names to lowercase on storage and lookup. This behavior also applies to database names and table aliases.

  • 2 - Table and database names are stored on disk using the lettercase specified in the CREATE TABLE or CREATE DATABASE statement, but MySQL converts them to lowercase on lookup. Name comparisons are not case sensitive. This works only on file systems that are not case sensitive! InnoDB table names are stored in lowercase, as for lower_case_table_names=1.

当然,您可以更改 lower_case_table_names 的值,但您需要导出/导入数据才能影响当前的数据库表。

关于PHP Mysql 不区分大小写的表选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11510316/

相关文章:

PHP/MYSQL AJAX 聊天

php - Smarty,页面/部分页面没有缓存

php - Laravel session 在每次加载时重新生成

mysql - 数据库设计日常数据

php - 通过 htaccess 将 PNG 解析为 PHP 仅适用于本地服务器,但不适用于网络服务器

php - 在 PHP 中的子构造函数之前调用父构造函数

python - centos上python 2.7专门安装MySQLdb

php - 如何获取具有不同键条件的行

mysql - 负载均衡级联 JDBCTap for MySQL

mysql - MySQL 数据库中可搜索的用户名列使用什么数据类型和索引?