Error Inserting the result of a SELECT into tables of different Mysql databases

Asked

Viewed 402 times

0

I’m getting an error when generating a select and then giving an input, I have 2 different banks but on the same server and I’m giving a select in the registered database whose table is record and giving an Insert to the target database which is the migrate_test whose table is sites the two tables have the same columns and types, my connection.php.

$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 funcao.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.php sites where the select and the 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 the F5 in the browser I have the error below:

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 input in the second record, if you put a LIMIT of 1 it generates the query and saves the input in the database

  • @Davidsam removed the penultimate ";" from this line and the error remained and if take out the last of the error in the lock of the key

  • is right. My comment was wrong, even deleted. What I think is coming from the first bank some character is breaking the Mysql query, as single quotes or double quotes.

1 answer

0


Good morning Henry. I have been looking at the code and realized that your goal is to insert the result of select in another table of another bank on the same server. If there is no need to work with the return of select you can use the following query:

INSERT INTO migracao_teste.sites (descricao, observacao)
SELECT
    registros.descricao AS descricao, 
    registros.observacao as observacao
FROM registro_duplicado.registro AS registros
ORDER BY registros.id'
  • Good morning Bilico, the return is not so important, the problem is that I need to concatenate the observation column, example, in it I have the domain information that today are like Uritiba and I need to add a subdomain, or it needs to be for example Curitiba.teste.localhost, the way you mentioned it works if I go to pass the information without concatenating anything, and I have many records to do one by one, I believe that to do this directly by sql there is no way, or there is some way?

  • can work with IF ELSE in select, example: if(records.Description != '' or records.Description is not null, Concat(record.Description, '.site.localhost'), '') Description and then use the same for observation

  • I close the balloon, that kills my problem, thanks for the help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.