I am getting an error when generating a select and then giving an insert, I have 2 different banks but on the same server and I am giving a select in the database double_dir (source database) whose table is a record and giving an insert to the
$servername = "localhost";
$username = "root";
$password = "";
$bdOrigem = 'registro_duplicado';
$bdDestino = 'migracao_teste';
$conn = mysql_connect($servername, $username, $password) or die("Falha na
conexão com o BD " . $conn->connect_error);
mysql_select_db($bdOrigem , $conn) or die('Não foi possível selecionar o BD
'. $bdOrigem );
A file called function.php.
function consulta($sql){
//gera o SQL
$result = mysql_query($sql);
//se o SQL est´acorreto
if (!$result) {die('SQL Invalido: ' . mysql_error());}
//se tem resultados
if (mysql_num_rows($result) > 0) {
//retorna os dados
return $result;
}else{
return false;
}
}
And the sites.php where I generate the select and insert.
require('conexao.php');
require('funcoes.php');
$sql = 'SELECT registros.id AS id, registros.descricao AS descricao,
registros.observacao as observacao
FROM registro_duplicado.registro AS registros
ORDER BY registros.id';
$registros = consulta($sql);
$insert = null;
if($registros){
while ($registro = mysql_fetch_array($registros)) {
$id = $registro['id'];
$descricao = ((!empty($registro['descricao'])) ?
utf8_encode($registro['descricao']) : '');
$observacao = ((!empty($registro['observacao'])) ? $registro['observacao']
. '.site.localhost' : '');
$insert .= "INSERT INTO sites (descricao, observacao) VALUES
('{$descricao}', '{$observacao}');";
}
} else {
echo 'Nenhum resultado encontrado';
}
if (!empty($insert)) {
print "<pre>";
print($insert);
print "</pre>";
mysql_select_db($bdDestino , $conn) or die('Não foi possível selecionar o BD
'.$bdDestino );
$result = mysql_query($insert);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
}
mysql_close($conn);
header('Content-Type: text/plain;');
print($insert);
When I give F5 in the browser I have the following error:
Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
'INSERT INTO sites (descricao, observacao) VALUES ('REGISTRO 2', 'teste.site.loca'
at line 1
And the error happens at the moment that it will give an insert in the second record, if it puts a LIMIT of 1 it generates the query and writes the insert in the bank