MysqlHelper
是一個便捷的通過PHP導(dǎo)入和導(dǎo)出Mysql數(shù)據(jù)庫表結(jié)構(gòu)和數(shù)據(jù)
的工具,可以快速實現(xiàn)mysql的數(shù)據(jù)庫的導(dǎo)入和導(dǎo)出.
mysqlli
擴展,開箱即用
長期穩(wěn)定
和持續(xù)更新
通過Composer導(dǎo)入類庫
composer require zjkal/mysql-helper
方式一: 常規(guī)方法
use zjkal\MysqlHelper;
$mysql = new MysqlHelper('root', 'root', 'testdatabase', '127.0.0.1', '3306', 'utf8mb4', 'wp_');
方式二: 實例化后,通過setConfig方法設(shè)置數(shù)據(jù)庫配置
$mysql = new MysqlHelper();
$mysql->setConfig(['username' => 'root', 'password' => 'root', 'database' => 'testdatabase']);
MysqlHelper針對常用的框架做了兼容,可以直接使用框架的數(shù)據(jù)庫配置, 比如ThinkPHP
框架或Laravel
框架
$mysql = new MysqlHelper();
$config = config('database.connections.mysql');
$mysql->setConfig($config);
//導(dǎo)出數(shù)據(jù)庫(包含表結(jié)構(gòu)和數(shù)據(jù))
$mysql->exportSqlFile('test.sql');
//僅導(dǎo)出數(shù)據(jù)庫表結(jié)構(gòu)
$mysql->exportSqlFile('test.sql', false);
//導(dǎo)出指定表的結(jié)構(gòu)和數(shù)據(jù)
$mysql->exportSqlFile('test.sql', true, ['table1', 'table2']);
__PREFIX__
占位符代替//導(dǎo)入數(shù)據(jù)庫
$mysql->importSqlFile('test.sql');
//導(dǎo)入數(shù)據(jù)庫,并自動替換表前綴
$mysql->importSqlFile('test.sql', 'wp_');
php8.1 用這個有bug 出現(xiàn)了
ErrorException: mysqli::real_escape_string(): Passing null to parameter
我把你的注釋了 改成這樣子了
// if ($withData) {
// // 導(dǎo)出表數(shù)據(jù)
// fwrite($outputFile, "-- 表數(shù)據(jù):$table\n");
// $result = $conn->query("SELECT FROM $table");
// while ($row = $result->fetch_assoc()) {
// dump($row);
// $columns = implode("','", array_map([$conn, 'real_escape_string'], array_values($row)));
// fwrite($outputFile, "INSERT INTO $table VALUES ('$columns');\n");
// }
// if (empty($row)) {
// fwrite($outputFile, "/ " . $table . "表沒有數(shù)據(jù) /\n");
// }
// }
if ($withData) {
// 導(dǎo)出表數(shù)據(jù)
fwrite($outputFile, "-- 表數(shù)據(jù):$table\n");
$result = $conn->query("SELECT FROM $table");
if ($result) { // 檢查查詢是否成功執(zhí)行
while ($row = $result->fetch_assoc()) {
$escapedValues = array_map(function ($value) use ($conn) {
return $conn->escape_string(strval($value));
}, $row);
$columns = implode("','", $escapedValues);
$insertStatement = "INSERT INTO $table VALUES ('$columns');\n";
fwrite($outputFile, $insertStatement);
}
if ($result->num_rows === 0) {
fwrite($outputFile, "/ " . $table . "表沒有數(shù)據(jù) /\n");
}
$result->free(); // 釋放結(jié)果集內(nèi)存
} else {
fwrite($outputFile, "/ 查詢失敗或者表不存在 /\n");
}
}
我已經(jīng)查到大概問題了, 也參考你的代碼做了一些修改, 跟php版本不大, 主要是這個函數(shù)mysql_real_escape_string, 莫名其妙的就是有問題, 也是從網(wǎng)上搜了挺多人遇到的錯誤才發(fā)現(xiàn)的. 所以不再使用它了.
1個G的sql,導(dǎo)入穩(wěn)定嗎?