3
<?
    $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?

1
  • Why don't you do it OO, which makes it possible for you to do $db1->query("SELECT * FROM db1.tags WHERE db1.catalog = 'fresh tag'"); instead. Commented Dec 8, 2010 at 20:30

7 Answers 7

8

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)

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

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

Comments

2

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);

Comments

1

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.

Comments

1

Use

mysql_query($query, $db1);

or

mysql_select_db();

Comments

0
//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/>;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.