gpt4 book ai didi

php - 使用 PHP 和 MySQL 重置数据库

转载 作者:行者123 更新时间:2023-11-30 01:31:46 24 4
gpt4 key购买 nike

我正在尝试编写一个重置数据库的脚本。我有一个正在使用的导出,可以使用 phpMyAdmin 导入。 SQL 文件与我正在使用的脚本位于同一位置。

当我运行此命令时,我很早就在文件中的 sql 文件中收到错误。

<?php
$mysqli = new mysqli('host', 'username', 'password', 'database');
$mysqli->query('SET foreign_key_checks = 0');
if ($result = $mysqli->query("SHOW TABLES"))
{
while($row = $result->fetch_array(MYSQLI_NUM))
{
$mysqli->query('DROP TABLE IF EXISTS '.$row[0]);
}
}

$mysqli->query('SET foreign_key_checks = 1');



$sql_filename = 'clean.sql';
$sql_contents = file_get_contents($sql_filename);
$result = $mysqli->query($sql_contents) or die("Database query failed: ".$mysqli->error);

$mysqli->close();

?>

最佳答案

您需要执行 multi_query() 来插入所有数据

$result = $mysqli->multi_query($sql_contents) or die("Database query failed: ".$mysqli->error);

看这里; http://php.net/manual/en/mysqli.quickstart.multiple-statement.php http://www.php.net/manual/en/mysqli.multi-query.php

关于php - 使用 PHP 和 MySQL 重置数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17366076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com