Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从excel文件插入时php mysql内存不足_Php_Mysql_Excel_Memory - Fatal编程技术网

从excel文件插入时php mysql内存不足

从excel文件插入时php mysql内存不足,php,mysql,excel,memory,Php,Mysql,Excel,Memory,我得到以下错误: 致命错误:第482行C:\xampp\htdocs\servisa\Excel\reader.php中允许的内存大小为33554432字节(尝试分配2字节) 我必须从excel文件中读取并插入mysql数据库。 我正在读取一个大的excel文件(13MB,9000行),将其放入一个数组中,然后在读取数组的同时生成插入查询,有没有一种方法可以用更少的内存来实现这一点? 我知道我可以在php.ini中设置更多moemory,但我认为这不是一个好的解决方案。是吗 这是我的密码,抱歉我

我得到以下错误:

致命错误:第482行C:\xampp\htdocs\servisa\Excel\reader.php中允许的内存大小为33554432字节(尝试分配2字节)

我必须从excel文件中读取并插入mysql数据库。 我正在读取一个大的excel文件(13MB,9000行),将其放入一个数组中,然后在读取数组的同时生成插入查询,有没有一种方法可以用更少的内存来实现这一点? 我知道我可以在php.ini中设置更多moemory,但我认为这不是一个好的解决方案。是吗

这是我的密码,抱歉我的英语太糟糕了

require_once 'Excel/reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1251');
$data->read('PAMI/PAMI.XLS');
$arreglo = array(array());
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) 
{
    if ($i == 1)
        continue;

    $nomCompleto = mysql_real_escape_string($data->sheets[0]['cells'][$i][3]);//str_replace("`", "", str_replace("'", "", $data->sheets[0]['cells'][$i][3]));
    $parts = explode(" ", $nomCompleto);
    $apellido = array_shift($parts);
    $nombre = implode(" ", $parts);
    $nroBenef = $data->sheets[0]['cells'][$i][1];
    $dni =  $data->sheets[0]['cells'][$i][5];

    $tipoDni = $data->sheets[0]['cells'][$i][4];
    $sexo = $data->sheets[0]['cells'][$i][8];
    $domicilio = $data->sheets[0]['cells'][$i][9];//mysql_real_escape_string($data->sheets[0]['cells'][$i][9].' '.$data->sheets[0]['cells'][$i][10]);

    $date = str_replace('/', '-', $data->sheets[0]['cells'][$i][6]);
    $fnac = date('Y-m-d', strtotime($date));

    $arreglo[$i][0] = $apellido;
    $arreglo[$i][1] = $nombre;
    $arreglo[$i][2] = $dni;
    $arreglo[$i][3] = $nroBenef;
    $arreglo[$i][4] = $tipoDni;
    $arreglo[$i][5] = $sexo;
    $arreglo[$i][6] = $domicilio;
    $arreglo[$i][7] = $fnac;
}   

$sql ="insert into clientespami (apellido,nombre,dni,nroBeneficiario,tipoDni,sexo,domicilio,fechaNac) values ";
$valuesArr = array();
foreach($arreglo as $fila)
{

    $ape = $fila[0];
    $nom = $fila[1];
    $dni = $fila[2];
    $ben = $fila[3];

    $tipo = $fila[4];
    $sex = $fila[5];
    $domi = $fila[6];
    $nac = $fila[7];

    $valuesArr[] = "('$ape', '$nom', '$dni','$ben','$tipo','$sex','$domi','$nac')";
}

$sql .= implode(',', $valuesArr);
//echo $sql;
mysql_query($sql,$conexion);
$cant = mysql_affected_rows();
require_once'Excel/reader.php';
$data=新的电子表格\u Excel\u阅读器();
$data->setOutputEncoding('CP1251');
$data->read('PAMI/PAMI.XLS');
$arreglo=array(array());
对于($i=1;$i工作表[0]['numRows'];$i++)
{
如果($i==1)
继续;
$nomCompleto=mysql\u real\u escape\u string($data->sheets[0]['cells'][$i][3])///str\u replace(“`,”,“”,$data->sheets[0]['cells'][$i][3]);
$parts=分解(“,$nomCompleto”);
$apellido=阵列移位($parts);
$nombre=内爆(“,$parts);
$nroBenef=$data->sheets[0]['cells'][$i][1];
$dni=$data->sheets[0]['cells'][$i][5];
$tipoDni=$data->sheets[0]['cells'][$i][4];
$sexo=$data->sheets[0]['cells'][$i][8];
$domicilio=$data->sheets[0]['cells'][$i][9];//mysql\u real\u escape\u string($data->sheets[0]['cells'][$i][9]。$data->sheets[0]['cells'][$i][10]);
$date=str_replace('/','-',$data->sheets[0]['cells'][$i][6]);
$fnac=日期('Y-m-d',标准时间($date));
$arreglo[$i][0]=$apellido;
$arreglo[$i][1]=$nombre;
$arreglo[$i][2]=$dni;
$arreglo[$i][3]=$nroBenef;
$arreglo[$i][4]=$tipoDni;
$arreglo[$i][5]=$sexo;
$arreglo[$i][6]=$domicilio;
$arreglo[$i][7]=$fnac;
}   
$sql=“插入客户SPAMI(apellido、nombre、dni、nroBeneficiario、tipoDni、sexo、domicilio、fechaNac)值”;
$valuesArr=array();
foreach($arreglo作为$fila)
{
$ape=$fila[0];
$nom=$fila[1];
$dni=$fila[2];
$ben=$fila[3];
$tipo=$fila[4];
$sex=$fila[5];
$domi=$fila[6];
$nac=$fila[7];
$valuesArr[]=“($ape'、$nom'、$dni'、$ben'、$tipo'、$sex'、$domi'、$nac')”;
}
$sql.=内爆(“,”,$valuesArr);
//echo$sql;
mysql\u查询($sql,$conexion);
$cant=mysql_受影响的_行();

警告:如果您只是在学习PHP,请不要学习过时的界面。这很糟糕,已经在PHP7中删除了。替换类和指南类有助于解释最佳实践。您的用户参数不正确,您的问题很严重。您能否转换为CSV并使用
加载数据填充
?感谢您的建议,我将调查PDO。CSV建议很好,但是,我认为我在执行插入之前得到了错误。我认为这与数组的大小有关,将所有内容加载到内存中,将其转换为SQL语句,然后将其发回只对少量数据有效。对于任何更大的,你真的需要它流。从CSV加载是最有效的,它完全绕过PHP,而且您不必担心注入问题,数据处理正确。但无论如何,我必须遍历所有excel文件以将其转换为CSV,对吗?