dongyied24121 2012-10-10 18:35
浏览 54
已采纳

高效的PHP代码将数组数据插入mysql表?

So I have a flatfile db in the format of username:$SHA$1010101010101010$010110010101010010101010100101010101001010:255.255.255.255:1342078265214

Each record on a new line... about 5000+ lines.. I want to import it into a mysql table. Normally I'd do this using phpmyadmin and "file import", but now I want to automate this process by using php to download the db via ftp and then clean up the existing table data and upload the updated db.

id(AUTH INCREMENT) | username | password | ip | lastlogin

The script I've got below for the most part works.. although php will generate an error: "PHP Fatal error: Maximum execution time of 30 seconds exceeded" I believe I could just increase this time, but on remote server I doubt I'll be allowed, so I need to find better way of doing this.

Only about 1000 records will get inserted into the database before that timeout...

The code I'm using is below.. I will say right now I'm not a pro in php and this was mainly gathered up and cobbled together. I'm looking for some help to make this more efficient as I've heard that doing an insert like this is just bad. And it really sounds bad aswel, as a lot of disk scratching when I run this script on local pc.. I mean why does it want to kill the hdd for doing such a seemingly simple task.

<?php
require ('Connections/local.php');

$wx = array_map('trim',file("auths.db"));
$username = array();
$password = array();
$ip = array();
$lastlogin = array();
foreach($wx as $i => $line) {

        $tmp = array_filter(explode(':',$line));
        $username[$i] = $tmp[0];
        $password[$i] = $tmp[1];
        $ip[$i] = $tmp[2];
        $lastlogin[$i] = $tmp[3];

mysql_query("INSERT INTO authdb (username,password,ip,lastlogin) VALUES('$username[$i]', '$password[$i]', '$ip[$i]', '$lastlogin[$i]') ") or die(mysql_error()); 
}
?>
  • 写回答

2条回答 默认 最新

  • dongqiao1888 2012-10-10 18:50
    关注

    Try this, with bound parameters and PDO.

    <?php
    require ('Connections/local.php');
    
    $wx = array_map('trim',file("auths.db"));
    $username = array();
    $password = array();
    $ip = array();
    $lastlogin = array();
    
    try {
        $dbh = new PDO("mysql:host=$ip;dbname=$database", $dbUsername, $dbPassword);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
    
    $mysql_query = "INSERT INTO authdb (username,password,ip,lastlogin) VALUES(:username, :password, :ip, :lastlogin)";
    $statement = $dbh->prepare($mysql_query);
    
    foreach($wx as $i => $line) {
            set_time_limit(0);
    
            $tmp = array_filter(explode(':',$line));
            $username[$i] = $tmp[0];
            $password[$i] = $tmp[1];
            $ip[$i] = $tmp[2];
            $lastlogin[$i] = $tmp[3];
    
            $params = array(":username"  => $username[$i],
                            ":password"  => $password[$i],
                            ":ip"        => $ip[$i],
                            ":lastlogin" => $lastlogin[$i]);
            $statement->execute($params);
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么