Ahojte
(na dnes druhy problem)
mam sql subor z exportu cez phpmyadmina, teda s create table a insert prikazmi a poznamkami
mate niekto hotovy php kod ktory by takyto subor vlozil do databazy?
Najskor to treba robit postupne nakolko cely sql subor sa neda importnut cez php (respektive nic take som nenasiel)
php import celeho sql suboru do databazy
-
caesar1987
Addict
- Príspevky: 3001
- Registrovaný: 02 okt 2005, 0:57
- Bydlisko: Nové Zámky
- Kontaktovať používateľa:
-
audiotrack
VIP
- Príspevky: 25958
- Registrovaný: 09 sep 2005, 18:39
- Kontaktovať používateľa:
Re: php import celeho sql suboru do databazy
Kód: Vybrať všetko
function mysql_import($filename) {
global $wpdb;
$return = false;
$sql_start = array('INSERT', 'UPDATE', 'DELETE', 'DROP', 'GRANT', 'REVOKE', 'CREATE', 'ALTER');
$sql_run_last = array('INSERT');
if (file_exists($filename)) {
$lines = file($filename);
$queries = array();
$query = '';
if (is_array($lines)) {
foreach ($lines as $line) {
$line = trim($line);
if(!preg_match("'^--'", $line)) {
if (!trim($line)) {
if ($query != '') {
$first_word = trim(strtoupper(substr($query, 0, strpos($query, ' '))));
if (in_array($first_word, $sql_start)) {
$pos = strpos($query, '`')+1;
$query = substr($query, 0, $pos) . $wpdb->prefix . substr($query, $pos);
}
$priority = 1;
if (in_array($first_word, $sql_run_last)) {
$priority = 10;
}
$queries[$priority][] = $query;
$query = '';
}
} else {
$query .= $line;
}
}
}
ksort($queries);
foreach ($queries as $priority=>$to_run) {
foreach ($to_run as $i=>$sql) {
$wpdb->query($sql);
}
}
}
}
}Stačilo pohľadať, zabralo mi to maximálne 2 minúty
-
caesar1987
Addict
- Príspevky: 3001
- Registrovaný: 02 okt 2005, 0:57
- Bydlisko: Nové Zámky
- Kontaktovať používateľa:
Re: php import celeho sql suboru do databazy
podobnu funkciu som skusal ale neberie posledny dotaz.. tak sa idem posnazit to vyriesit 
dakujem
//edit
tak to co som ja skusal malo v sebe viac chyb ked som to porovnal s tou ktoru si mi sem dal.
Tu stacilo na konci kontrolovat ze ci nie je v query este nieco a vlozit to do pola
dakujem
//edit
tak to co som ja skusal malo v sebe viac chyb ked som to porovnal s tou ktoru si mi sem dal.
Tu stacilo na konci kontrolovat ze ci nie je v query este nieco a vlozit to do pola
Kód: Vybrať všetko
function mysql_import($filename) {
global $db_import;
$return = false;
$sql_start = array('INSERT', 'UPDATE', 'DELETE', 'DROP', 'GRANT', 'REVOKE', 'CREATE', 'ALTER');
$sql_run_last = array('INSERT');
if (file_exists($filename)) {
$lines = file($filename);
$queries = array();
$query = '';
if (is_array($lines)) {
foreach ($lines as $line) {
$line = trim($line);
if(!preg_match("'^--'", $line)) {
if (!trim($line)) {
if ($query != '') {
$first_word = trim(strtoupper(substr($query, 0, strpos($query, ' '))));
$priority = 1;
if (in_array($first_word, $sql_run_last)) {
$priority = 10;
}
$queries[$priority][] = $query;
$query = '';
}
} else {
$query .= $line;
}
}
}
if ($query != '') {
$first_word = trim(strtoupper(substr($query, 0, strpos($query, ' '))));
$priority = 1;
if (in_array($first_word, $sql_run_last)) {
$priority = 10;
}
$queries[$priority][] = $query;
$query = '';
}
ksort($queries);
foreach ($queries as $priority=>$to_run) {
foreach ($to_run as $i=>$sql) {
echo $sql . "<br />";
$db_import->query($sql);
}
}
}
}
}