PHP 脚本不会转储 SQL 但 phpMyAdmin 会

标签 php mysql

我尝试通过 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/

相关文章:

php - 如何只允许从我的服务器上的文件调用 PHP 脚本?

php - webmin php-lib.pl 修改

php - 在 Prestashop 1.7 的管理产品页面中添加新字段的正确方法

MySQL:如何在一个查询中使用分配表连接三个表?

MySQL select id 其中至少有一个值不在列表中

mysql - 用mysql统计第一名分数的个数

mysql - MariaDB 无法在 FreeBSD 10 上启动

php - 我如何从 MySQL 数据库中渲染 PHP 代码?

mysql - 为什么 count(column_name) 会这样?

php - $request 中的已上传文件