-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathread_sql.php
More file actions
51 lines (36 loc) · 1.14 KB
/
read_sql.php
File metadata and controls
51 lines (36 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
* Пример php функции для загрузки sql файла в базу построчно
*/
//ini_set("memory_limit", "512M");
$arStartSkipSymbols = ["--","/*"];
try {
$dbh = new PDO('mysql:host=localhost;dbname=mytest', "root", "");
// Read in entire file
$handle = @fopen("geo_city_2013_09.sql", "r");
if ($handle) {
$c = 0;
$sql = "";
while (($line = fgets($handle, 4096)) !== false) {
if (empty(trim($line)) || in_array(substr($line,0,2),$arStartSkipSymbols) ) continue;
$sql.= $line;
//формируем запросы из строк
if ( substr(trim($line),-1)==';'){
//echo '['.$sql.']<br>';
//if ($c++>30) break;
$res = $dbh->exec( $sql );
echo ++$c.' - '.$res.'<br>';
$sql = "";
}
}
//if (!feof($handle)) {
// echo "Error: unexpected fgets() fail\n";
//}
unset($dbh);
fclose($handle);
}
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>