php - json解码函数后奇怪的sql插入

标签 php mysql json decode

解码 jsonfile 并插入到 mysql 后,我的数据库中得到了奇怪的输入结果。 使用 echo 函数,我得到每个变量 $clanid 的正确输出。但在数据库中我总是得到每个 id 条目的数字 2147483647。 所有其他变量和条目都很好。 有人知道这里发生了什么吗?

这是代码:

$con = mysqli_connect("localhost","xxxx","xxxx","xxxxx") or die("Error " . mysqli_error($con));

$url = "http://185.112.249.77:9999/Api/Search?search=&level=1&min=1&max=50&points=48000";
$jsondata = file_get_contents($url);
$data = json_decode($jsondata, true);

$in=$data['clanList'];
$results = $data['results'];
$i = 0;
while($i + 1 <= $results){
    $clanid = $in[$i]['id'];
    $clanname = mysqli_real_escape_string($con,$in[$i]['name']);
    echo "Clan ID: $clanid<br />Clan Name: $clanname <br /><br />";

    $clanplayerCount = $in[$i]['playerCount'];
    $clanwarswon = $in[$i]['warsWon'];
    $clanwarslost = $in[$i]['warsLost'];
    $clanwarstied = $in[$i]['warsTied'];
    $clanLocation = $in[$i]['clanLocation'];
    $clanlevel = $in[$i]['level'];
    $clanidcorrected = $clanid;

    $sql = "INSERT INTO activeclans(id, name, location, playercount, clanlevel, warswon, warslost, warstied)
    VALUES('$clanidcorrected', '$clanname', '$clanLocation', '$clanplayerCount', '$clanlevel', '$clanwarswon', '$clanwarslost', '$clanwarstied')";

    mysqli_query($con, $sql); //must pass the connection variable before the sql query.
    $i++;
}

这是使用 php 文件后的结果: http://bornhoffer.de/grab-active-clans-search.php

所以..回显结果很好..

但在数据库中我得到这些条目:

 2147483647 ARAB CHAMPIONS? United Arab Emirates    50  6   43  23  11
 2147483647 Emirates    United Arab Emirates    50  5   23  18  8
 2147483647 Kings Rock  International   49  7   106 59  26
 2147483647 MEGA EMPIRE International   49  5   50  40  14
 2147483647 NORTH 44    International   49  5   70  39  10
 2147483647 Quantum's Web   U.S. Virgin Islands 50  5   63  23  9
 2147483647 ??? ??????? International   50  6   166 19  6

任何帮助将不胜感激。 提前致谢。

最佳答案

您在数据库中的 id 列上使用 INTEGER,并且 json 文件中的数字太大,无法放入其中,您可以切换您的 id 列到 BIGINT

2147483647是mysql中最大的INTEGER值我相信,你链接中的所有数字http://bornhoffer.de/grab-active-clans-search.php比那还大。

关于php - json解码函数后奇怪的sql插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33245485/

相关文章:

mysql - 使用 Node 收集 mysql 查询

php - pdo 准备插入失败

json - 向 JSON 对象添加新的键值对

php - 优化数据可视化 Web 应用程序的性能

php - CKEditor 样式不起作用

php - 无法从 Windows 7 64 位 Apache 2.4 上的 PHP 5.5.4 连接到 LDAPS 服务器

PHP在多维数组中添加元素

php - curl 和 cookies

MySQL - 表查询内部连接到自身

javascript - 通过 javascript 对象属性值过滤 JSON 响应