MySQL多查询将无法正常工作

时间:2012-12-06 17:00:05

标签: php mysql sql mysqli

我使用以下脚本:

<?php

ini_set('max_execution_time', 0);

ini_set('error_reporting', E_ALL);

$file = file_get_contents('./dump.sql');

//var_dump($file); die();

$conn = new mysqli('localhost', 'myUserName', 'myPassWord', 'myDataBase');

if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

echo 'Success... ' . $conn->host_info . "<br />";
echo 'Retrieving dumpfile' . "<br />";

$sql = $file;
if (!$sql){
    die ('Error opening file');
}

echo 'processing file <br />';
mysqli_multi_query($conn,$sql);

echo 'done.';
$conn->close();

?>

它输出以下内容,所有内容似乎都很顺利,完全没有错误:

Success... localhost via TCP/IP
Retrieving dumpfile
processing file
done.

但是,我的数据库中没有字段。什么都没有添加。

我的dump.sql文件中的第一行代码如下:

-- phpMyAdmin SQL Dump
-- version 3.4.5
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Dec 05, 2012 at 04:01 PM
-- Server version: 5.5.16
-- PHP Version: 5.3.8

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `myDataBase`
--

-- --------------------------------------------------------

--
-- Table structure for table `content_field_image_cache`
--

CREATE TABLE IF NOT EXISTS `content_field_image_cache` (
  `vid` int(10) unsigned NOT NULL DEFAULT '0',
  `nid` int(10) unsigned NOT NULL DEFAULT '0',
  `delta` int(10) unsigned NOT NULL DEFAULT '0',
  `field_image_cache_fid` int(11) DEFAULT NULL,
  `field_image_cache_list` tinyint(4) DEFAULT NULL,
  `field_image_cache_data` text,
  PRIMARY KEY (`vid`,`delta`),
  KEY `nid` (`nid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `content_field_image_cache`
--

INSERT INTO `content_field_image_cache` (`vid`, `nid`, `delta`, `field_image_cache_fid`, `field_image_cache_list`, `field_image_cache_data`) VALUES
(1000, 1000, 0, 1000, 1, NULL),
(1001, 1001, 0, 1001, 1, NULL),
(1002, 1002, 0, 1002, 1, NULL),
(1003, 1003, 0, 1003, 1, NULL),

为什么这段代码不起作用?感谢。

2 个答案:

答案 0 :(得分:2)

似乎您需要遍历结果以消除MySQL server has gone away.错误:

以最简单的形式(未经测试的例子):

mysqli_multi_query($conn,$sql);

// You can do something with the results as well...
while (mysqli_use_result($conn) && mysqli_next_result($conn));

echo 'done.';
$conn->close();

答案 1 :(得分:0)

尝试用分号“;”结束插入而不是你的最后一个元组之后的逗号。