David Hynek
PHP import souboru databáze vytvořené v PhpMysqlAdmin
php
<?php
session_start();
header('Content-type: text/html; charset=utf-8');
class Db_importer
{
private $report;
private $mysqli;
function __construct($sql_database, $sql_server, $sql_uzivatel, $sql_heslo) {
$this->mysqli = new mysqli($sql_server, $sql_uzivatel, $sql_heslo, $sql_database);
$this->mysqli->query("SET GLOBAL max_allowed_packet=10485760");
}
function prijata_data()
{
$sql = file_get_contents($_FILES['soubor']['tmp_name']);
if ($this->mysqli->connect_errno) {
$this->report = "Chyba při připojení do databáze: <br />".$this->mysqli->connect_error;
}
if (!$this->mysqli->multi_query($sql)) {
$this->report = "Chyba při ukládání do databáze:<br />".$this->mysqli->error;
}
do {
if ($res = $this->mysqli->store_result()) {
var_dump($res->fetch_all(MYSQLI_ASSOC));
$res->free();
}
} while ($this->mysqli->more_results() && $this->mysqli->next_result());
if($this->report == "") $this->report = "Data byla do databáze úspěšně nahrána. Celkem bylo uloženo ".(number_format((($_FILES['soubor']['size'])/1024), 2, ',', ' '))."kB dat.";
return $this->report;
}
function formular()
{
if(isset($_POST["form_ok"])) {
$this->prijata_data();
return $this->report;
}
else
{
return "<form method='post' enctype='multipart/form-data'>
<span>Soubor s databází max. 10MB </span><input type='file' name='soubor' />
<input type='submit' name='form_ok' value='Vložit' />
</form>";
}
}
function __destructor() {$this->mysqli->close($link);}
}
$db_imp = new Db_importer("nazev_databaze","server","uzivatel","heslo");
echo $db_imp->formular();
?>
Neformátovaný
Přidáno: 21.4.2013
Expirace: Neuvedeno