PHP Insert data script into database

Source: Internet
Author: User

PHP Insert data script into database

<?php


Add_user_pwd ();


/**

* Add password account to database

*/

function Add_user_pwd ()

{

/* Connect MySQL */

$type = ' MySQL ';

$dbname = ' Jihua ';

$host = ' 127.0.0.1 ';

$username = ' root ';

$password = ' root ';

$DSN = "$type: host= $host;d bname= $dbname";

try {

$pdo = new PDO ($DSN, $username, $password);

} catch (Exception $e) {

Die (' MySQL connection error ');

}


/* Read File information */

$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 ');

}

}

/* Close Database */

Fclose ($file);


Echo '----------------------------------<br> ';

Echo '----------------------------------<br> ';

Echo '----------------------------------<br> ';

Echo '----------------------------------<br> ';

Echo '----------------------------------<br> ';

Echo '----------------------------------<br> ';

Echo '----------------------------------<br> ';

Echo ' total: '. $i. ' Data ';

}


This article is from "Linux Fairy Road" blog, please be sure to keep this source http://8999a.blog.51cto.com/11385098/1963637

PHP Insert data script into database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.