English 中文(简体)
利用php从茨夫档案中收集的人口数据库
原标题:populating database from csv file using php
  • 时间:2011-10-31 01:23:28
  •  标签:
  • php

我将如何利用PHP代码,把一个信息输入一个笼罩上的数据库? 我需要实践利用Sphp来进行数据库电话,但目前,我都能查阅这一卷宗。

最佳回答

仅通过phpmyadmin:

问题回答

Design Considerations: You probably don t want to load the entire file into memory at once using a function like file_get_contents. With large files this will eat up all of your available memory and cause problems. Instead do like Adam suggested, and read one line at a time.

fgetcsv at php Manual

//Here s how you would start your database connection
mysql_connect($serverName, $username, $password);
mysql_select_db( yourDBName );

//open the file as read-only
$file = fopen("file.csv", "r");

// lineLength is unlimited when set to 0
// comma delimited
while($data = fgetcsv($file, $lineLength = 0, $delimiter = ",")) {
   //You should sanitize your inputs first, using a function like addslashes
   $success = mysql_query("INSERT INTO fileTable VALUES(".$data[0].",".$data[1].")");
   if(!$success) {
     throw new Exception( failed to insert! );
   }
}

利用现成的房贷功能读作特别志愿人员,并撰写一份产出文件。 然后,你可以把这套材料输入你的数据库。 这应当与任何类型的数据库合作。

不要忘记你正在使用的任何扼杀。 在这方面,我利用了诉讼地——替罪羊。

$fd = fopen("mydata.csv", "r");
$fdout = fopen("importscript.sql","w");

while(!feof($fd))
{
    $line = fgetcsv($fd, 1024); // Read a line of CSV

    fwrite($fdout, INSERT INTO mytable (id,name) 
                 . VALUES ( .intval($line[0]).", ".sqlite_escape_string($line[1])." );
";
}

fclose($fdout);
fclose($fd);




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签