Q: Create table on master with data from slave in MySQL 5.1 and PHP 5.4 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.

A: 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 . "`) ");
    }
}

