如何使用json数据自动使用PHP中的数千个数据更新MySQL列

问题描述 投票:0回答:4

我有一个带有列的“用户”表:

Id | user_id | name | ip_address | lat | lng | active

纬度(lat)和经度(lng)列是空的,我需要用大概位置填充它们(我不需要精确的位置)。使用这个论坛,在另一个问题中,我发现我必须分离IP,并使用位置库。好。我创建了一个提取IP的函数,并将IP2Location库添加到我的项目中。如果我为IP获取IP,获取经度和经度,并在MySQL中逐个添加它,它有效,但是,我有125,000条记录,并且没有办法手动完成这项工作。谁会知道怎么做?

使用此功能

function List_ip() {
    global $sqlConnect, $db;
    if ($db['loggedin'] == false) {
        return false;
    }

    $ips = mysqli_query($sqlConnect, "SELECT `user_id` , `ip_address` FROM " . T_USERS . " WHERE `active` = '1'");
    while ($fetched_data = mysqli_fetch_assoc($ips)) {
     $list_ip[] = array_map('utf8_encode', $fetched_data); 
    }    

   return $list_ip;

}

我有以下json结果

{
     "api_status": 200,
     "ips": [
         {
             "user_id": "1",
             "ip_address": "177.198.86.7x"
         },
         {
             "user_id": "21",
             "ip_address": "177.18.246.9x"
         },
         {
             "user_id": "52",
             "ip_address": "177.36.60.1x"
         }
     ]
}

我有这个脚本可以给我基于IP的经度和纬度

<?php
require_once './assets/libraries/vendor/ip2location/ip2location-php/IP2Location.php';

$reader = new \IP2Location\Database('./IP2LOCATION-LITE-DB9.BIN', \IP2Location\Database::FILE_IO);

$records = $reader->lookup('8.8.8.8', \IP2Location\Database::ALL);

echo '<pre>';
echo 'IP Number             : ' . $records['ipNumber'] . "\n";
echo 'Latitude              : ' . $records['latitude'] . "\n";
echo 'Longitude             : ' . $records['longitude'] . "\n";

?>

如何自动为每个IP插入纬度和经度数据?

对不起代码量,我想尽可能清楚。

php mysql json loops
4个回答
1
投票

所以这里是获取不同(唯一)IP地址的函数,并将它们传递给第二个函数以获得Lat / Long并更新数据库。

function List_ip() {
    global $sqlConnect, $db;
    if ($db['loggedin'] == false) {
        return false;
    }

    $ips = mysqli_query($sqlConnect, "SELECT DISTINCT `ip_address` FROM " . T_USERS . " WHERE `active` = '1'");
    while ($fetched_data = mysqli_fetch_assoc($ips)) {
       ip2LocationAndUpdate($fetched_data["ip_address"]); //passing to other function
    }
}

全球的东西

require_once './assets/libraries/vendor/ip2location/ip2location-php/IP2Location.php';
$reader = new \IP2Location\Database('./IP2LOCATION-LITE-DB9.BIN', \IP2Location\Database::FILE_IO);

执行IP2Location和更新sql的第二个函数

function ip2LocationAndUpdate($ip_address){
    global $sqlConnect, $reader;
    $records = $reader->lookup($ip_address, \IP2Location\Database::ALL);

    $updateSQL = "UPDATE " . T_USERS . " SET `lat` = '$records["latitude"]', `lat` = '$records["latitude"]' WHERE `ip_address` = '$ip_address'";

    //execute the sql with mysqli or whatever you are using
}

0
投票

如果要插入的行太多,我建议创建一个在后台运行的cron任务,它更快,也不影响你的网站(万一你有)

搜索crontab。或者shell_exec

然后,如果您想显示进度,可以使用广播或推送器


0
投票

Nawed Khan的回答通过一些小修改解决了我的问题。这是我目前运行100%的代码。感谢大家的回答,这对我学习PHP语言非常有用。

function List_ip() {
    global $sqlConnect, $db;
    if ($db['loggedin'] == false) {
        return false;
    }

    $ips = mysqli_query($sqlConnect, "SELECT DISTINCT `ip_address` FROM " . T_USERS . " WHERE `lat` = '0'");
    while ($fetched_data = mysqli_fetch_assoc($ips)) {
       ip2LocationAndUpdate($fetched_data["ip_address"]); //passing to other function
    }
}

 require_once 'assets/libraries/vendor/ip2location/ip2location-php/IP2Location.php';

 $reader = new \IP2Location\Database('IP2LOCATION-LITE-DB9.BIN', \IP2Location\Database::FILE_IO);

function ip2LocationAndUpdate($ip_address){
    global $sqlConnect, $reader;
    $records = $reader->lookup($ip_address, \IP2Location\Database::ALL);

     //execute the sql with mysqli or whatever you are using

    $latitu = $records['latitude'];
    $longitu = $records['longitude'];

    $updateSQL = mysqli_query($sqlConnect, "UPDATE " . T_USERS . " SET `lat` = '$latitu', `lng` = '$longitu' WHERE `ip_address` = '$ip_address'");


    return $updateSQL;


}

-2
投票

使用流动算法来解决您的问题

<?php
for($i=1;$i<125000;$i++)
{
 // here run what you want to do
}
?>

注意:如果您使用在线网站的服务器,在1000记录停止10秒后

© www.soinside.com 2019 - 2024. All rights reserved.