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

  • SEARCH
  • Home
  • 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 6927815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:06:15+00:00 2026-05-27T11:06:15+00:00

In MySQL 5.1 and PHP 5.4 I need to create temporary tables from the

  • 0

In MySQL 5.1 and PHP 5.4 I need to create temporary tables from the result of a query on a slave database. The problem is that I need the temporary table to be created on the Master (with data from the slave). It’s the selection of the data for this table that carries all the overhead so I need the SELECT to happen on one of the slaves. The temporary table will be selected from for up to 2 hours, and I can’t copy it to ALL the slaves (at least I don’t think I can).

Here is what the code looks like:

$database->executeQuery ( "CREATE TABLE IF NOT EXISTS `" . $tableName . "` ENGINE = $engine CHARACTER SET utf8 ( " . $sql . ") " );

Again, the query in the $sql variable has to happen on the slave, while the table is created on the master.

  • 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-27T11:06:15+00:00Added an answer on May 27, 2026 at 11:06 am

    Ok, turns out the way for me was to grab the result set in PHP from the slave, then create a SQL statement to insert it into the master.

     public function createTemporaryTable ($tableName, $keyField = '', $sql = '', $engine = 'MEMORY') {
        global $apdatabase;
        $dbObj = new apdatabase();
        $dbObj->setSqlCache(false);
        $dbObj->setSqlBigResult(true);
        $dbObj->setSqlCalcFoundRows(false);
        $dbObj->setResultMode(MYSQLI_USE_RESULT);
    
        $sql = $sql ? $sql : $this->getQuery();
        $dbObj->executeQuery($sql);
        $partsArr = array();
        $foundRows = false;
        $resultArr = $dbObj->getResultArray();
        $dbObj->freeResultSet();
        if (! count($resultArr)) {
            return;
        }
        /*
         * Set up the statement we will use to create the table definition
        */
        $firstRow = $resultArr[0];
        $fields = '';
        $fieldDefinitionStr = '';
        foreach ($firstRow as $field => $data) {
            if ($fields != "") {
                $fields .= ",";
                $fieldDefinitionStr .= ",";
            }
            $fields .= $field;
            $fieldDefinitionStr .= "`" . $field . "` VARCHAR (1024)";
        }
        parent::executeQuery("DROP TABLE IF EXISTS `" . $tableName . "`");
        parent::executeQuery("CREATE TABLE `" . $tableName . "`  ( " . $fieldDefinitionStr . ") ENGINE = $engine CHARACTER SET utf8");
    
        /*
         * Set up the sql to insert the remaining rows into our new table
         */
        $sql = "INSERT IGNORE INTO `$tableName` ($fields) ";
        $partsArr = array();
        foreach ($resultArr as $row) {
            $foundRows = true;
            $rowSql = "(";
            foreach ($row as $field) {
                if ($rowSql != "(") {
                    $rowSql .= ",";
                }
                $rowSql .= "'" . $field . "'";
            }
            $rowSql .= ") ";
            $partsArr[] = $rowSql;
        }
        parent::executeQuery($sql . "VALUES " . implode(', ', $partsArr));
    
        if ($keyField) {
            parent::executeQuery("  ALTER TABLE `" . $tableName . "` ADD KEY (`" . $keyField . "`) ");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a menu with PHP from a MySQL database. Table called
I need to join two tables from different MySQL (PHP) connection strings and different
I need to create two related tables in MySQL and return a result set
I need to create 3 HTML columns in PHP with data returned from MySQL.
I have a php file which connects to mysql database. I need to create
I need to create MySQL tables that represent a tree structure like this: Root
I am trying to create some pages using PHP / MySQL. My database table
We need to create a program in PHP/MySQL that will display all the 'people',
I am newbee with PHP and MySQL and need help... I have two tables
I need to add values received from MySQL into an array (PHP). Here is

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.