Jump to content

mySQLi statement for multiple databases


TapeGun007
Go to solution Solved by Barand,

Recommended Posts

I have mySQLi statement that looks similar to this:

$sql = "SELECT  ID, FirstName, Last Name, Email, Mobile, PromoCode 
            FROM table1
            WHERE (Email LIKE ?)
            OR (Mobile LIKE ?)
            OR (PromoCode LIKE ?)
            ";
    $stmt = $consd->prepare($sql);
    $stmt->bind_param('sss', $Search,$Search, $Search);

 

Works fine.  But what I cannot seem to find is what if I had two databases with all the same tables and I want to query both of them using mySQLi?

 

I've tried all types of things, but I *think* because it's older mysql code that I'm not getting the syntax quite right...

Link to comment
Share on other sites

  • Solution

Prefix the table name with the database name

$sql = "SELECT  ID, FirstName, Last Name, Email, Mobile, PromoCode 
            FROM database1.table1
            WHERE (Email LIKE ?)
            OR (Mobile LIKE ?)
            OR (PromoCode LIKE ?)
        UNION
        SELECT  ID, FirstName, Last Name, Email, Mobile, PromoCode 
            FROM database2.table1
            WHERE (Email LIKE ?)
            OR (Mobile LIKE ?)
            OR (PromoCode LIKE ?)
            ";

Databases must be on the same server.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.