php 往数据库插入数据脚本
<?php
add_user_pwd();
/**
* 添加密码账户到数据库
*/
function add_user_pwd()
{
/* 连接mysql */
$type = 'mysql';
$dbname = 'jihua';
$host = '127.0.0.1';
$username = 'root';
$password = 'root';
$dsn = "$type:host=$host;dbname=$dbname";
try {
$pdo = new PDO($dsn, $username, $password);
} catch(Exception $e) {
die('MYSQL连接错误');
}
/* 读取文件信息 */
$file = fopen("test1.txt", "r");
$i = 0;
while(! feof($file)) {
if(($i % 100) == 0) {
$pdo->exec('set autocommit=0');
}
list($username, $password) = explode(' ',fgets($file));
$username = trim($username);
$password = trim($password);
$sql = "insert into gc_user (`username`,`pwd`)
VALUES('{$username}','{$password}')";
echo $sql.'<br>';
$pdo->exec($sql);
if((++$i%100) == 0) {
flush();
$pdo->exec('set autocommit=1');
}
}
/* 关闭数据库 */
fclose($file);
echo '----------------------------------<br>';
echo '----------------------------------<br>';
echo '----------------------------------<br>';
echo '----------------------------------<br>';
echo '----------------------------------<br>';
echo '----------------------------------<br>';
echo '----------------------------------<br>';
echo '总共:'.$i.' 数据';
}
本文转自去轻狂书生51CTO博客,原文链接: http://blog.51cto.com/8999a/1963637,如需转载请自行联系原作者