Connecting multiple database and join query across database in php

后端 未结 2 1267
灰色年华
灰色年华 2020-12-21 11:07

Using php i wants to make a join query across 2 database.

This is my first connection.

$conn = mysql_connect(\'localhost\',\'root1\',\'pass1\'); 
@my         


        
相关标签:
2条回答
  • 2020-12-21 11:18

    You can do this by preceding the table name also with the database name, like you proposed in the example. But the logged on user needs to have access to both databases under the same credentials. This last part is very important, else you won't be able to do it.

    This gives you an easy but straightforward example: link

    Example taken from that link (will only work with same credentials):

    SELECT
        c.customer_name,
        o.order_date
    FROM
        db1.tbl_customers c LEFT JOIN
        db2.tbl_orders o ON o.customer_id = c.id
    
    0 讨论(0)
  • 2020-12-21 11:24

    The mysql module in the php environment doesn't support multiple database connections, and, besides, this module exists only for retrocompatibily reasons. You shouldn't use it.

    mysqli and PDO are better, more complete (these support transaction and prepared statement, "mysql" don't), and PDO support also the multi-db query.

    The old mysql is procedural only, mysqli can be used in procedural and OO programming, PDO is OOP.

    0 讨论(0)
提交回复
热议问题