开发者

PHP multi database connect

<?
    $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 a multi database, how to make a MySQL query from db1?


Specify it in the query.

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

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


Look in the manual.

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

Like you already do for mysql_select_db(), you can specify the connection as the second parameter.


The second parameter of mysql_query is the connection resource. it will query against that connection.


See the mysql_query documentation. You just pass in link_identifier as the second parameter.

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


If they are both in localhost you do not need to connect 2 times. you could simply do something like this:

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

This way you can compare between both Databases without making two connections.

Now if you do not have both in the same place then 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 ones from $db2Result.


Use

mysql_query($query, $db1);

or

mysql_select_db();


//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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜