My code processes a .txt file that contains approximately 50,000 clients, which I must enter into a table using the MySQL batching mechanism "LOAD DATA LOCAL INFILE" (Bulk Insert in the Sql Server's world or MongoDb)
My code works fine if I do it from the same browser, but if I do the same with an Ajax call, it returns me "false", so I imagine that an apache or PHP security reason blocks me from executing this procedure. Does anyone know what I should do?
My code is:
<?php
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
error_reporting(E_ALL);
ini_set("display_errors", 1);
$mysqli = new mysqli('127.0.0.1', 'user_name', 'secret', 'prueba');
$file = $_POST["file"];
if ($mysqli->connect_errno) {
echo "Errno: " . $mysqli->connect_errno . "
";
echo "Error: " . $mysqli->connect_error . "
";
exit;
}
$sql = "LOAD DATA LOCAL INFILE '/var/www/node/importacion/api/csv_files/{$file}' INTO TABLE cuentas FIELDS TERMINATED BY '|'";
try {
$resp = $mysqli->query($sql);
$mysqli->close();
$respuesta = array(
"server" => $resp
);
}catch (Exception $e){
print_r($e->getMessage());
}
echo json_encode($respuesta);
Not an answer yet, but debugging. AJAX
is expecting a JSON
string, and with errors, you are providing simple TEXT which might screw things up and you're missing the error messages.
Update your php:
$respuesta = '';
if ($mysqli->connect_errno) {
$respuesta .= "Errno: " . $mysqli->connect_errno . "
";
$respuesta .= "Error: " . $mysqli->connect_error . "
";
// exit;
}
...
catch (Exception $e){
$respuesta .= 'Exception Error: '.print_r($e->getMessage(),1);
}
Update your AJAX:
$.ajax(
{
data : "file="+archivo,
type : "post",
dataType: "json",
url : "http://localhost/procesarBulk.php",
success : function(data){
if(data.server==false){
alert("Hemos tenido problemas para procesar el lote : "+
JSON.stringify(data)
);
}else{
alert("Bien, finalizamos todo");
}
},
error: function( jqXHR, textStatus, errorThrown) {
alert(JSON.stringify({
title: "Connection Error",
typeAnimated: true,
content: "There was a connection problem with "+this.url+'=> ('+textStatus+'):'+errorThrown+". Please try again"
}));
});
See what extra info you get.
ALSO check your web server log for errors