找到你要的答案

Q:How to store LONGTEXT to MySql database

Q:如何存储到mysql数据库中单引号字符

I have a very large matrix 1328x1328 and I'm trying to store the upper triangle (it is symmetrical) of it to the database as LONGTEXT. In JavaScript I serialize it: JSON.stringify(matrix) (serialized string length 16744056 characters) and send it to the server using ajax:

$.ajax({
      type: "PUT",
      url: 'post-user-matrix.php',
      data: JSON.stringify(matrix),
      contentType: "application/json"
    });

It arrives to PHP with no problems, but from there I want to save it to the database table

u_id(INT) | matrix_json (LONGTEXT)

The PHP script that should send the data to the database look like this:

$user_id = getUserID();
$matrix = file_get_contents("php://input");

if ($insert_stmt = $mysqli->prepare("INSERT INTO users_matrix (u_id, matrix_json) VALUES (?,?)")) {
    $null = NULL;
    $insert_stmt->bind_param('is',$user_id, $null);

    $matrixchunks = str_split($matrix, 8192);

    for ($i=0 ; $i < count($matrixchunks) ; $i++) {
        $insert_stmt->send_long_data(1, $matrixchunks[$i] );
    }

    $insert_stmt->execute(); 

}

I tried to use send_long_data relying on PHP documentation, but the script gives no errors and doesn't send anything to the database.

I have a very large matrix 1328x1328 and I'm trying to store the upper triangle (it is symmetrical) of it to the database as LONGTEXT. In JavaScript I serialize it: JSON.stringify(matrix) (serialized string length 16744056 characters) and send it to the server using ajax:

$.ajax({
      type: "PUT",
      url: 'post-user-matrix.php',
      data: JSON.stringify(matrix),
      contentType: "application/json"
    });

它的到来对PHP没有问题,但我想把它保存到数据库表

u_id(int)| matrix_json(单引号字符)

PHP脚本,应该将数据发送到数据库看起来像这样:

$user_id = getUserID();
$matrix = file_get_contents("php://input");

if ($insert_stmt = $mysqli->prepare("INSERT INTO users_matrix (u_id, matrix_json) VALUES (?,?)")) {
    $null = NULL;
    $insert_stmt->bind_param('is',$user_id, $null);

    $matrixchunks = str_split($matrix, 8192);

    for ($i=0 ; $i < count($matrixchunks) ; $i++) {
        $insert_stmt->send_long_data(1, $matrixchunks[$i] );
    }

    $insert_stmt->execute(); 

}

我试图用send_long_data依托的PHP文件,但是脚本没有错误和不发送任何数据库。

answer1: 回答1:

I think the problem is in flags - has to be ib, not is

 $insert_stmt->bind_param('ib',$user_id, $null);

It can be seen on php documentation sample here: http://php.net/manual/en/mysqli-stmt.send-long-data.php

我认为问题是旗帜-要IB,不是

 $insert_stmt->bind_param('ib',$user_id, $null);

它可以在这里看到http://php.net/manual/en/mysqli-stmt.send-long-data.php PHP文档示例:

php  mysql