2 connections to Mysql in the same PHP

0

I have a problem with connections to mysql currently from a PHP I establish connections to mysql in the following way

<?php 
  $date = strftime( "%Y-%m-%d %H:%M:%S"); //FECHA
  function conectarse($host,$usuario,$password,$BBDD){ 
    $link=@mysql_connect($host,$usuario,$password) or die (mysql_error()); 
    mysql_select_db($BBDD,$link) or die (mysql_error()); 
    return $link; 
  } 

  $link=conectarse("localhost","usuario1","123456","basededatos");  

  $con_at = "select * from tabla";
  $con_at = mysql_query($con_at,$link);
  $con = mysql_fetch_array($con_at);
  $atdb_server = $con['at_dbserver'];
  $atdb_user = $con['at_dbuser'];
  $atdb_pass = $con['at_dbpass'];

  function conectarse2($host,$usuario,$password,$BBDD){ 
    $link2=@mysql_connect($host,$usuario,$password) or die ('error 2da db'); 
    mysql_select_db($BBDD,$link2) or die (mysql_error()); 
    return $link2; 
  } 
  $link2=conectarse2($atdb_server,$atdb_user, $atdb_pass,"base2");  

  $sql = "SELECT * FROM config_camp where cp_tipo = '0' ";
  $data =mysql_query($sql, $link);
  $afectadas = mysql_num_rows(mysql_query($sql, $link)); 

The problem is that the query ($ sql) does not work, (it does it towards the first connection). But if I delete the second connection if it works. I need to keep both connections since later I have to make queries to both. Does anyone have any idea how to do it? Hope it's understandable. Thanks in advance.

    
asked by Lusag silva 27.07.2018 в 21:53
source

1 answer

2

First, it is my duty to point out that the mysql extension is obsolete and can not be used in modern php versions. That in addition to having several security flaws.

Secondly, the function mysql_connect , when executed a second time to the same server (even if they are different databases or schemas) can return the first connection resource instead of creating a new one. In theory that would only happen if you use the same user, but apparently there are border cases.

If this were the case, what is happening is:

  • You create the connection 1
  • Fix database 1 for connection 1
  • You create connection 2 but PHP returns a reference to connection 1
  • You set database 2 for connection 2 which implies that you changed the database below for connection 1.

A short and dirty solution would be to use the fourth parameter that supports mysql_connect that indicates if you want to force the creation of a new link:

$link2=@mysql_connect($host,$usuario,$password, true) or die ('error 2da db'); 

I do not know that it works and I do not have a way to prove it because all the PHP sites I run are already running in PHP 7. However, the default behavior of the mysql_xxxx() functions when you do not pass the connection resource is assumed as connection the last valid connection you got with mysql_connect . It may be that somewhere in your code you are not specifying the connection resource. Without going any further, when you call mysql_error() you are omitting the resource.

In your case, I would seriously evaluate changing to a more modern driver. For example, PDO_MySQL supports specifying the database on the DSN of the connection , even if you are using the same username and password.

$link1 = new PDO("mysql:host=localhost;dbname=$db_name",'usuario1','password1');
$link1->setAttribute("PDO::ATTR_ERRMODE", PDO::ERRMODE_EXCEPTION);

$link2 = new PDO("mysql:host=localhost;dbname=$db_name2",'usuario1','password1');
$link2->setAttribute("PDO::ATTR_ERRMODE", PDO::ERRMODE_EXCEPTION);

$res1 = $link1->query("SELECT option_value FROM wp_options WHERE option_name='siteurl'", PDO::FETCH_ASSOC);

$res2 = $link2->query("SELECT option_value FROM wp_options WHERE option_name='siteurl'", PDO::FETCH_ASSOC);

Changing from one driver to another can be tedious if your code base is very large, but you can implement it gradually.

    
answered by 28.07.2018 в 01:00