简体   繁体   English

错误的SQL用PHP创建临时表

[英]Error SQL create temp table with php

I try to select multiple values from a table, playlist_generate, with a condition, create a temp table, update a field and than insert in my table 我尝试从一个表中选择多个值playlist_generate,并附带条件,创建一个临时表,更新一个字段,然后将其插入到我的表中

$data_tabella_duplicata = $_POST['data_duplicata'];
$data_iniziale_originale = $_GET['data_iniziale'];

$query_duplica_playlist = "
DROP TABLE IF EXISTS temp_table;
CREATE TEMPORARY TABLE temp_table LIKE playlist_generate;
SELECT data_playlist, giorno_playlist, orario_playlist, nome_evento,nome_programma FROM playlist_generate WHERE data_playlist = '".$data_iniziale_originale."';
UPDATE temp_table SET data_playlist='".$data_tabella_duplicata."';
INSERT INTO playlist_generate SELECT null,data_playlist, giorno_playlist,orario_playlist, nome_evento, nome_programma FROM temp_table;
DROP TABLE temp_table;
";

$esegui_query_duplica_playlist = $connessione->query($query_duplica_playlist);
if ($connessione->error) {
try {
    throw new Exception("MySQL error $connessione->error <br> Query:<br> $query_duplica_playlist", $connessione->errno);
} catch (Exception $e) {
    echo "Error No: ".$e->getCode()." - ".$e->getMessage()."<br >";
    echo nl2br($e->getTraceAsString());
}
}

but i have an error I don't understand 但我有一个我不明白的错误

Error No: 1064 - MySQL error You have an error in your SQL syntax; 错误编号:1064-MySQL错误您的SQL语法有误。 check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TEMPORARY TABLE temp_table LIKE playlist_generate; 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'CREATE TEMPORARY TABLE temp_table LIKE playlist_generate附近使用; SELECT data_playlist, ' at line 2 SELECT data_playlist,第2行

I try the code in phpmyadmin and it works 我尝试在phpmyadmin中的代码,它的工作原理

if I use this 如果我用这个

$query_duplica_playlist = "
DROP TABLE IF EXISTS temp_table;
CREATE TEMPORARY TABLE temp_table ENGINE = MEMORY;
SELECT data_playlist, giorno_playlist, orario_playlist, nome_evento,nome_programma FROM playlist_generate WHERE data_playlist = '".$data_iniziale_originale."';
UPDATE temp_table SET data_playlist='".$data_tabella_duplicata."';
INSERT INTO playlist_generate SELECT null,data_playlist, giorno_playlist,orario_playlist, nome_evento, nome_programma FROM temp_table;
DROP TABLE temp_table;
";

$esegui_query_duplica_playlist = $connessione->multi_query($query_duplica_playlist);

I have no result 我没有结果

From the docs - 文档 -

You cannot use CREATE TEMPORY TABLE ... LIKE to create an empty table based on the definition of a table that resides in the mysql tablespace , InnoDB system tablespace (innodb_system), or a general tablespace. 您不能使用CREATE TEMPORY TABLE ... LIKE来基于位于mysql表空间 ,InnoDB系统表空间(innodb_system)或常规表空间中的表的定义创建空表 The tablespace definition for such a table includes a TABLESPACE attribute that defines the tablespace where the table resides, and the aforementioned tablespaces do not support temporary tables. 此类表的表空间定义包括TABLESPACE属性,该属性定义了表所在的表空间,并且上述表空间不支持临时表。

In addition, you appear to be trying to run multiple queries at once. 此外,您似乎正在尝试一次运行多个查询。 If you're using MySQLi you will want to use multi_query() . 如果您使用MySQLi,则需要使用multi_query() Using multi_query() , especially in a situation such as this is not ideal so consider your logic carefully should you find yourself wanting to use this function. 使用multi_query() ,尤其是在这种情况下并不理想,因此,如果发现自己想要使用此函数,请仔细考虑您的逻辑。

Thanks to all. 谢谢大家。 So the best way to do this is make more queries. 因此,执行此操作的最佳方法是进行更多查询。 Thanks 谢谢

// 1 query
$query_seleziona_data_controllo        = "SELECT data_generata FROM data_generata WHERE data_generata ='".$data_tabella_duplicata."' ";
$esegui_query_seleziona_data_controllo = $connessione->query($query_seleziona_data_controllo);

$numero_righe_query_controllo = $esegui_query_seleziona_data_controllo->num_rows;
if ($numero_righe_query_controllo == 0) {

// 2 query
$query_duplica_playlist = "INSERT INTO playlist_generate_temp
SELECT null,data_playlist, giorno_playlist, orario_playlist, nome_evento,nome_programma FROM playlist_generate
WHERE data_playlist = '".$data_iniziale_originale."' ";

$esegui_query_duplica_playlist = $connessione->query($query_duplica_playlist);

// 3 query
$query_update_data_nuova = "UPDATE playlist_generate_temp SET data_playlist='".$data_tabella_duplicata."' WHERE data_playlist = '".$data_iniziale_originale."' ";

$esegui_query_update_data_nuova = $connessione->query($query_update_data_nuova);

// 4 query
$query_inserisci_duplicato = "INSERT INTO playlist_generate
SELECT null,data_playlist, giorno_playlist, orario_playlist, nome_evento,nome_programma FROM playlist_generate_temp
WHERE data_playlist = '".$data_tabella_duplicata."' ";

$esegui_query_inserisci_duplicato = $connessione->query($query_inserisci_duplicato);

// 5 query
$query_truncate_temp = "TRUNCATE TABLE playlist_generate_temp";

$esegui_query_truncate_temp = $connessione->query($query_truncate_temp);

mysqli_close($connessione);

} else {

echo "";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM