Skip to content

Instantly share code, notes, and snippets.

@bytefade
Created August 20, 2014 14:09
Show Gist options
  • Save bytefade/6da6bf564dd9f5dbdbd5 to your computer and use it in GitHub Desktop.
Save bytefade/6da6bf564dd9f5dbdbd5 to your computer and use it in GitHub Desktop.
Insert plano exemplo otimizado
<?php
require_once 'Excel/reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1251');
$data->read('exemplo.xls');
//constantes com os dados do db.
define('DB_NOME', '*******');
define('DB_USUARIO', '********');
define('DB_SENHA', '***********');
define('DB_HOST', '*******');
//conecta ao MySQL.
$conexao = mysql_connect(DB_HOST, DB_USUARIO, DB_SENHA) or die ('Erro ao conectar ao MySQL: ' . mysql_error());
//conectar ao db 'flashInteracao'.
$conexaoDb = mysql_select_db(DB_NOME, $conexao) or die ('Erro ao conectar a base de dados: ' . mysql_error());
$userData = array();
for ($x = 1; $x <= count($data->sheets[0]["cells"]); $x++) {
$userData[] = '(
"' .$data->sheets[0]["cells"][$x][1] . '",
"' .$data->sheets[0]["cells"][$x][2] . '",
"' .$data->sheets[0]["cells"][$x][3] . '",
"' .$data->sheets[0]["cells"][$x][4] . '",
"' .$data->sheets[0]["cells"][$x][5] . '",
"' .$data->sheets[0]["cells"][$x][6] . '",
"' .$data->sheets[0]["cells"][$x][7] . '",
"' .$data->sheets[0]["cells"][$x][8] . '",
"' .$data->sheets[0]["cells"][$x][9] . '",
"' .$data->sheets[0]["cells"][$x][10] . '",
"' .$data->sheets[0]["cells"][$x][11] . '")';
}
$query = 'INSERT INTO rcsaude_tabela_preco (`tiulo`, `faixa1`, `faixa2`, `faixa3`, `faixa4`, `faixa5`, `faixa6`, `faixa7`, `faixa8`, `faixa9`, `faixa10`) VALUES' . implode(',', $userData);
$result = mysql_query($query);
// trata erro
if (!$result) {
die("erro: ".mysql_error());
}
// Verifique o resultado
echo 'Consulta v&aacute;lida: ' . $query . '<br>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment