【问题标题】:Weird sql insert after json decode functionjson解码功能后奇怪的sql插入
【发布时间】:2015-10-20 19:42:36
【问题描述】:

解码一个 jsonfile 并插入到 mysql 后,我的数据库中得到了奇怪的输入结果。 使用回声功能,我得到每个变量 $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

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

【问题讨论】:

  • 请为activeclans 表显示您的CREATE TABLE 声明?

标签: php mysql json decode


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 2018-02-12
    • 1970-01-01
    • 2021-07-11
    • 2021-12-19
    • 2021-05-20
    相关资源
    最近更新 更多