dousendun8411 2017-10-04 22:30
浏览 106

PHP和Ajax:无法从Ajax调用执行LOAD DATA LOCAL INFILE(mysql)

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);
  • 写回答

1条回答 默认 最新

  • douzhang2092 2017-10-05 02:13
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题