system with 2 databases , returning empty mysql php queries

Asked

Viewed 107 times

1

I have a system where I have a user base which is a unified base in a bank, and another base in the system. In my pages I call them so:

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

Follows below 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>");

Plus my appointments are returning empty. Follow the 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 must I do to make it right?

1 answer

4


As you are working with two banks it is mandatory to inform in which of them you want to perform the operation(Insert, update, delete select etc) otherwise PHP will get the last open connection. To resolve this enter which database/connection you want to use in mysql_query().

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

Job signature:

Mixed mysql_query ( string $query [, Resource $link_identifier = NULL ] )

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

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

Related:

Manual - mysql_query

  • Hi perfect, that’s right. About PHP7 I’m already studying the PDO. Still, Thanks for the help.

Browser other questions tagged

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