Asked  7 Months ago    Answers:  5   Viewed   40 times

How Can I use this 2 connections to run a query which joins 2 tables? yesno.table1 and sushi.table1 ? lets say we join by id they both have the same id. how can I do that?

// Connect to Yesno Database
$this->yesno_db = new mysqli("red", "yesno", "***", "yesnotmp");

if ($this->yesno_db->connect_errno) {
    throw new Exception('Connection failed: '.$this->yesno_db->connect_error);
}   

// Connect to Sushi Database
$this->sushi_db = new mysqli("red", "sushi", "***", "sushi");

if ($this->sushi_db->connect_errno) {
    throw new Exception('Connection failed: '.$this->sushi_db->connect_error);
}

 Answers

63

This question has nothing to do with mysqli (or any other API).

To do a join with a table from different database, a user which connects to mysql, have to have SELECT rights for both databases.

Having this done, just select one of databases in your connection statement and address another using usual dot syntax:

SELECT * FROM t1 JOIN db2.t2

To answer your question literally,

Can I use this 2 connections to run a query which joins 2 tables?

You can't

Wednesday, March 31, 2021
 
rblarsen
answered 7 Months ago
74

From the PHP Manual on mysqli_stmt::execute:

mysqli_stmt::execute -- mysqli_stmt_executeExecutes a prepared Query

Returns TRUE on success or FALSE on failure.


if ($stmt->execute()) { // exactly like this!
    $success = true;
}

You're doing it right... What's your dilemma?

Wednesday, March 31, 2021
 
Sauleil
answered 7 Months ago
71

This is a FAQ for InnoDB tables. See the explanation at https://phpmyadmin.readthedocs.org/en/latest/faq.html?highlight=MaxExactCount#the-number-of-rows-for-innodb-tables-is-not-correct

Saturday, May 29, 2021
 
Student
answered 5 Months ago
13

Yes, assuming the account has appropriate permissions you can use:

SELECT <...>
FROM A.table1 t1 JOIN B.table2 t2 ON t2.column2 = t1.column1;

You just need to prefix the table reference with the name of the database it resides in.

Tuesday, June 1, 2021
 
Stefan
answered 5 Months ago
58

You could call it just style, but I prefer aliasing to improve readability.

UPDATE A    
  SET ControllingSalesRep = RA.SalesRepCode   
from DHE.dbo.tblAccounts A
  INNER JOIN DHE_Import.dbo.tblSalesRepsAccountsLink RA
    ON A.AccountCode = RA.AccountCode

For MySQL

UPDATE DHE.dbo.tblAccounts A 
  INNER JOIN DHE_Import.dbo.tblSalesRepsAccountsLink RA 
      ON A.AccountCode = RA.AccountCode 
SET A.ControllingSalesRep = RA.SalesRepCode
Wednesday, July 28, 2021
 
Manju
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share