thinkphp如何批量設置mysql的字符集
發布時間:2024-03-19
|
浏覽次數:2989次
描述
思路是首先将項目的數據庫設置成想要的字符集,再使用Db::getTables獲取到所有的數據表,循環數據表然後使用Db::execute執行設置爲想要的字符集
詳情
以下(xià)代碼演示了如何将一(yī)個項目的數據表,批量設置成自己想要的字符集。
<?php namespace app\test\controller; use app\index\common\Base as BaseController; use think\facade\Db; class Index extends BaseController { public function index() { $tables = Db::getTables(); $database_name = 'qianhong'; $sql = "ALTER DATABASE ".$database_name." CHARACTER SET utf8 COLLATE utf8_general_ci"; $res = Db::execute($sql); foreach ($tables as $table) { $sql = "ALTER TABLE $table CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci"; $res = Db::execute($sql); } } }