system with 2 databases, returning empty queries mysql php

1

I have a system where I have the user base that is a unified database in one database, and another system based on the system. In my pages I call them like this:

<?php
include("conn_user.php");
include("conn_sys.php");
?>

Here are the connections:

  

include ("conn_user.php");

$host1       = 'localhost';
$dbuser1     = 'root';
$dbpassword1 = '';
$dbname1     = 'cadastro';

$conn_user = @mysql_connect($host1, $dbuser1, $dbpassword1) or die ("Não foi possível conectar-se ao servidor MySQL");
$db1 = @mysql_select_db($dbname1) or die ("Não foi possível selecionar o banco de dados <b>$dbname1</b>");

$host2       = 'localhost';
$dbuser2     = 'root';
$dbpassword2 = '';
$dbname2     = 'eos';
  

include ("conn_sys.php");

$host2       = 'localhost';
$dbuser2     = 'root';
$dbpassword2 = '';
$dbname2     = 'eos';

$conn_user = @mysql_connect($host2, $dbuser2, $dbpassword2) or die ("Não foi possível conectar-se ao servidor MySQL");
$db2 = @mysql_select_db($dbname2) or die ("Não foi possível selecionar o banco de dados <b>$dbname2</b>");

More only that my queries are returning empty.  Here is an example of a query:

<div class="form-group">
  <label for="select" class="col-lg-4 control-label">SETOR:</label>
  <div class="col-lg-8">
  <select type="text" class="form-control" name="setor" id="setor">
  <option selected value=''></option>
  <?php
  $setor = "SELECT * FROM setor ORDER BY setor_desc ASC";
    while ($dados = mysql_fetch_array($setor)) {
    echo("<option value='".$dados['setor_id']."'>   ".$dados['setor_id']."   -   ".$dados['setor_desc']."   </option>");
    }
     ?>
    </select>
    </div>
  </div>

What should I do to get it right?

    
asked by anonymous 02.08.2016 / 20:38

1 answer

4

As you are working with two banks it is mandatory to inform in which one of them wants to perform the operations (insert, update, delete select etc) otherwise PHP will get the last connection open. To resolve this, please tell which bank / connection you want to use in mysql_query() .

Note that the connection is always the second argument or it is the inverse of mysqli.

Signature of the function:

  

mixed mysql_query (string $ query [ resource $ link_identifier = NULL])

$result = mysql_query('select ... from ...', $banco1);
$result2 = mysql_query('select ... from ...', $banco2);

Note that these functions have already been removed from PHP7 so please update your code with mysqli or PDO.

Related:

Manual - mysql_query

    
02.08.2016 / 20:42