Connecting to multiple PHP databases

<?
    $db1 = mysql_connect("localhost","root","root") or dir("can not connect Mysql Server");
    mysql_select_db("1",$db1) or dir("Can not connect to the MySQL Server.");

    $db2 = mysql_connect("localhost","root","root") or dir("can not connect Mysql Server");
    mysql_select_db("2",$db2) or dir("Can not connect to the MySQL Server.");
?>

$result = mysql_query("SELECT * FROM db1.tags WHERE db1.catalog='fresh tag' ");

If I connect from multiple databases, how do I make a MySQL query from db1?

+3
source share
7 answers

Indicate it in the request.

$result = mysql_query($query, $db1);

If the second parameter ($ db1) is omitted, it will use the last defined resource ($ db2)

+8
source

See the manual .

resource mysql_query ( string $query [, resource $link_identifier ] )

As you have already done for mysql_select_db(), you can specify the connection as the second parameter.

+2
source
+2

. mysql_query. link_identifier .

$result = mysql_query("SELECT * FROM db1.tags WHERE db1.catalog='fresh tag'", $db1);
+2

mysql_query($query, $db1);

mysql_select_db();
+1

, 2 . - :

SELECT * FROM db2.tags WHERE id IN(SELECT id FROM db1.tags WHERE id=1)

This way you can compare both databases without two connections.

Now, if you do not have both in one place, you can do something like this:

mysql_select_db(db1);
$db1Result = mysql_query("SELECT * FROM db1.tags WHERE id=1");
mysql_select_db(db2);
$db2Result = mysql_query("SELECT * FROM db2.tags WHERE id=1");

And after that, compare the results from $ db1Result with the tags from $ db2Result.

+1
source
//get my conn
include "../../database.php";
//put data in
//get data
if($_GET['attribute']!=''){
    //prepare
    $stmt=$conn->prepare('INSERT INTO databasetablename (attribute) VALUES (?)');
    //bind
    $stmt->bind_param('s',$_GET['attribute']);
    //execute
    $stmt->execute();
    $stmt->close();
}

    //print data out
    $res=$conn->query("SELECT attribute FROM databasetablename");
    if($res){
        while($hold = mysqli_fetch_array($res,MYSQL_ASSOC)){
            $record[]=$hold;
            echo $hold['attribute'];
        }
    }
    echo '<hr/>';
    //print out
        foreach($record as $cur){
            echo $cur['attribute'].<br/>;
        }
0
source

Source: https://habr.com/ru/post/1779261/


All Articles