Consultation with connection in more than one bank php mysql

0

I have 3 databases and I'm looking for information in all three, but in my first query I've come across a one error:

It is as if the query was searching in another bank that is not what I chose, here follows my query, the error message and my connection file.

  

Inquiry:

 $query_pesquisa = mysql_query(" SELECT 
    o.numero AS CHAMADO,
    p.problema AS PROBLEMA,
    o.descricao AS DESCRICAO,
    o.contato AS CONTATO,
    o.telefone AS TELEFONE,
    o.data_abertura AS DATA_DE_ABERTURA,
    TIMESTAMPDIFF (HOUR, str_to_date(data_abertura,'%Y-%m-%d'), CURDATE()) as HORAS_EM_ABERTO,
    i.inst_nome AS UNIDADE,
    a.sistema AS AREA,
    l.local AS SETOR,
    u.nome AS TECNICO,
    ua.nome AS ABERTO_POR,
    s.status AS STATUS
    /*sls.slas_tempo AS TEMPO_DE_SOLUCAO*/
FROM
    ocorrencias AS o
        LEFT JOIN
    sistemas AS a ON a.sis_id = o.sistema
        LEFT JOIN
    localizacao AS l ON l.loc_id = o.local
        LEFT JOIN
    instituicao AS i ON i.inst_cod = o.instituicao
        LEFT JOIN
    usuarios AS u ON u.user_id = o.operador
        LEFT JOIN
    usuarios AS ua ON ua.user_id = o.aberto_por
        LEFT JOIN
    'status' AS s ON s.stat_id = o.status
        LEFT JOIN
    status_categ AS stc ON stc.stc_cod = s.stat_cat
        LEFT JOIN
    problemas AS p ON p.prob_id = o.problema
        LEFT JOIN
    sla_solucao AS sls ON sls.slas_cod = p.prob_sla
        LEFT JOIN
    prioridades AS pr ON pr.prior_cod = l.loc_prior
        LEFT JOIN
    sla_solucao AS slr ON slr.slas_cod = pr.prior_sla
        LEFT JOIN
    script_solution AS sol ON sol.script_cod = o.oco_script_sol
        LEFT JOIN
    prior_atend AS prioridade_atendimento ON prioridade_atendimento.pr_cod = o.oco_prior
WHERE
        a.sistema = 'Ti'
        AND o.status IN (1 , 2)
ORDER BY numero ASC",$ConnOcomon)or die(mysql_error());

    if (empty($query_pesquisa)) {
    echo "Nenhum registro encontrado.";
    }
    $num_rows = mysql_num_rows($query_pesquisa);
  

Connection:

<?php
   header('Content-Type: text/html; charset=utf-8');

 //BANCO FOLLOWUP
$local_serve1       = "127.0.0.1";  
$usuario_serve1     = "root";     
$senha_serve1       = "";             
$banco_de_dados1    = "bigchamados";      
$ConnChamados       = mysql_connect($local_serve1,$usuario_serve1,$senha_serve1) or die ("O servidor não responde!");
$db1                = mysql_select_db($banco_de_dados1,$ConnChamados)            or die ("Não foi possivel conectar-se ao banco de dados!"); 

// BANCO OCOMON
$local_serve2       = "127.0.0.1";  
$usuario_serve2     = "root";     
$senha_serve2       = "";             
$banco_de_dados2    = "ocomon_rc6";      
$ConnOcomon         = mysql_connect($local_serve2,$usuario_serve2,$senha_serve2) or die ("O servidor não responde!");
$db2                = mysql_select_db($banco_de_dados2,$ConnOcomon)              or die ("Não foi possivel conectar-se ao banco de dados!");

//BANCO USUÁRIOS
$local_serve3       = "127.0.0.1";
$usuario_serve3     = "root";
$senha_serve3       = "";
$banco_de_dados3    = "bigcadastro";
$ConnUser           = mysql_connect($local_serve3,$usuario_serve3,$senha_serve3) or die ("O servidor não responde!");
$db3                = mysql_select_db($banco_de_dados3,$ConnUser)                or die ("Não foi possivel conectar-se ao banco de dados!");


  mysql_query("SET NAMES 'utf8'");
  mysql_query('SET character_set_connection=utf8');
  mysql_query('SET character_set_client=utf8');
  mysql_query('SET character_set_results=utf8'); 
?> 
  

Error message:

Table 'bigcadastro.ocorrencias' doesn't exist
    
asked by anonymous 02.09.2016 / 17:36

3 answers

2

First question and check the database, its internal structure. Since it has not been posted I will assume that the ocomon_rc6 database actually has the ocorrencias table. Second, the code you posted is incomplete, you should post the whole code so you can understand its logic.

But basically it is the following, the bank that you are using does not have the table that you are accessing (obviously) of the two ones:

  • You are passing the wrong connection to perform this query;
  • You are inadvertently ignoring the link_identifier of the connection, so PHP uses the last link opened by mysql_connect () in the documentation available here you can check this behavior:
      

    link_identifier The MySQL connection. If the identifier link is not specified, the last link opened by mysql_connect () is used. If   a previous connection does not exist it will be attempted to create one from a   chadama to mysql_connect () without any arguments. If no connection is   found or set, an E_WARNING level error is generated.

  • 02.09.2016 / 19:21
    -1

    The care you must take in systems with more than one DB is that it logically becomes a system with several CONNECTIONS .

    What I see is the data and all connecting at the same time, so the connection file include.

    My first suggestion: separate the connections, give the include of the main, and when to use the others, do not forget to use mysqli_close() .

    My second suggestion: If it's the case that ALL are connected at the same time, I'm afraid you should take a look at webservers , so data competition is viewed in a more organized way. In English, the PHP site shows something with concurrent connections

    My third suggestion: websockets . As his conception of the word says:

      

    is a technology that allows bi-directional full-duplex channel communication over a single Transmission Control Protocol (TCP) socket. It is designed to run on browsers and web servers that support HTML5, but can be used by any client or application server.

        
    02.09.2016 / 19:21
    -1

    There is not much to complement, but the fact of not looking in the right place is why it is not indicating the place to be sought,

    Example:

    $sql = '...';
    
    // Para buscar no banco bigchamados
    mysql_query( $sql, $db1 );
    
    // Para buscar no banco ocomon_rc6
    mysql_query( $sql, $db2 );
    
    // Para buscar no banco bigcadastro
    mysql_query( $sql, $db3 );
    
        
    03.09.2016 / 02:39