Under_Control;10930978 wrote:but it's not csv file it's txt
A CSV file is a text file. As I stated in my first reply, you can use the optional 3rd argument to fgetcsv to use a character other than the comma as your field delimiter, allowing you to use it to parse, for instance, tab-delimited files, or in this case colon-delimited.
Untested:
if($fh = fopen('path/to/file'))
{
while(($line = fgetcsv($fh, 9999, ':') !== false)
{
if(count($line) >= 2)
{
$sql = sprintf(
"INSERT INTO table_name (`name`, `phone`) VALUES('%s', '%s')",
mysql_real_escape_string(trim($line[0])),
mysql_real_escape_string(trim($line[1]))
);
mysql_query($sql);
}
}
}
If the file is large, you might want to add some additional logic to create an arbitrary number of VALUES() groups for the INSERT statement, so that you only execute 1 query for every n rows in the file.