Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3282706
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:57:29+00:00 2026-05-17T19:57:29+00:00

I am trying to use PHP and MySQL’s Create Table Select between two different

  • 0

I am trying to use PHP and MySQL’s Create Table Select between two different MySQL servers. I am not sure this can be done like this with SQL. I get no errors but I get nothing done either:

<?php
$dbname = 'cms';
$dbmaster = 'cmsms';

$db1 = mysql_connect('localhost', 'root', 'secret');
if (!$db1) {
    echo 'Could not connect to mysql';
    exit;
}

$db2 = mysql_connect('server2', 'root', 'secret');
if (!$db2) {
    echo 'Could not connect to mysql';
    exit;
}

mysql_select_db("$dbname", $db1) or die ("Unable to select database");
mysql_select_db("$dbmaster", $db2) or die ("Unable to select database");

$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql, $db1);

if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}

while ($row = mysql_fetch_row($result)) {
    $sql = "DROP TABLE `$row[0]`";
    mysql_query($sql, $db1);
    echo "Table rows: {$row[0]} Deleted <br/>";
    $sql = "CREATE TABLE $row[0] SELECT * FROM $db2.$dbmaster.$row[0] ";
    mysql_query($sql, $db1);
    echo "Table: {$row[0]} created <br/>";

}
echo "<br/>done...";

mysql_free_result($result);
?>

That line:

$sql = "CREATE TABLE $row[0] SELECT * FROM $db2.$dbmaster.$row[0] ";

just doesn’t work. the $db2 doesn’t make it go to the other server and select.

From doing some reading on SO I found someone similar and someone said it could not be done and to look at federated tables, which will not work for me.

If this cannot be done above does anyone know a way to do what I am doing? I am dropping the tables on the copy and re-creating them based on the table in the master. Then I am selecting the data in the master to put in the re-created tables. Thank you

Update:
Just so I can be clear. The code works if everything were on the same server and I only had one database connection. It is because of the create table select that I have problems, I believe. This SQL needs to use two servers at the same time. The create table is for one database that just dropped it’s tables but the select is selecting from the database of the second connection – two connections in the same SQL statement.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:57:29+00:00Added an answer on May 17, 2026 at 7:57 pm

    Yes, on your PHP script you can perform all queries you want on all different servers you have access and permission to.

    You don’t specify your database on the $sql though. You specify it when you run the mysql_query function.

    So, your code:

    $sql = "SHOW TABLES FROM $dbname";
    $result = mysql_query($sql, $db1);
    

    Is wrong. It should only be:

    $sql = "SHOW TABLES";
    $result = mysql_query($sql, $db1);
    

    By using $db1 on the second parameter of mysql_query you are already specifying that you want to show tables from that database only.

    Following the same reasoning on the other sentences, you should be able to get results you want.

    You won’t be able to do this:

    $sql = "CREATE TABLE $row[0] SELECT * FROM $db2.$dbmaster.$row[0] ";
    

    If you want to insert rows from $db2 into newly created tables into $db1, you need to:

    1. Run a query on $db2 to get results
    2. Create table on $db1
    3. Iterate $db2 results
    4. Insert records on new table on $db1

    There is no magic way of doing this in only one sentence.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to use SQLite with the PDO wrapper in PHP with mixed
While trying to use LINQ to SQL I encountered several problems. I have table
Trying to use an excpetion class which could provide location reference for XML parsing,
Trying to use a guid as a resource id in a rest url but
I' trying to use a Linq query to find and set the selected value
I trying to use ImageInfo and Jython to get information from a image on
I'm trying to use svnmerge.py to merge some files. Under the hood it uses
I'm trying to use SQLBindParameter to prepare my driver for input via SQLPutData .
I'm trying to use jQuery to format code blocks, specifically to add a <pre>
I'm trying to use the Optiflag package in my Ruby code and whenever I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.