gpt4 book ai didi

PHP 脚本不会转储 SQL 但 phpMyAdmin 会

转载 作者:行者123 更新时间:2023-11-29 06:59:10 31 4
gpt4 key购买 nike

我尝试通过 php 脚本转储我的数据,但没有命令行。所以我用 this script 创建了我的 .sql 文件然后我尝试使用我的脚本:

$link = mysql_connect($host, $user, $pass);
mysql_select_db($name, $link);
$sqlFile = 'sql.sql';
$fsize = filesize($sqlFile);
$fopen = fopen($sqlFile, 'r');
$fread = fread($fopen, $fsize);
fclose($fopen);
mysql_query($fread) or die(mysql_error());
mysql_close();

当我尝试转储数据时出现错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE `ps_access` (`id_profile` int(10) unsigned NOT NULL,`id_tab` int(10' at line 1

但是当我将 sql 粘贴到 phpMyAdmin sql 输入选项卡时,此 sql 代码没有问题:

DROP TABLE IF EXISTS `ps_access`;
CREATE TABLE `ps_access` (
`id_profile` int(10) unsigned NOT NULL,
`id_tab` int(10) unsigned NOT NULL,
`view` int(11) NOT NULL,
`add` int(11) NOT NULL,
`edit` int(11) NOT NULL,
`delete` int(11) NOT NULL,
PRIMARY KEY (`id_profile`,`id_tab`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

那么我在 php 脚本中的错误在哪里以及如何修复它以转储数据?

平均 SQL 文件约为 800KB

最好的问候,乔治!

最佳答案

错误发生是因为mysql_query不支持多查询:

mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

( source )

理论上您可以使用 explode + 一个 foreach 循环来分别发送每个查询,但实际上这可能无法按预期工作。参见:Loading .sql files from within PHP

关于PHP 脚本不会转储 SQL 但 phpMyAdmin 会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11075972/

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