Sto provando ad eseguire il codice SQL sotto dalla mia classe PHP, ma quando lo faccio dà un errore. Il codice seguente funziona perfettamente nella console di PHPMyAdmin, ma non in PHP.Codice PHP per eseguire più query con una sola chiamata mysql_query()
SET @columns := (
SELECT
GROUP_CONCAT(column_name)
FROM information_schema.columns
WHERE table_schema = 'test'
AND table_name = 'mytable'
AND column_key <> 'PRI'
);
SET @sql := (
SELECT CONCAT(
'INSERT INTO mytable (', @columns, ') ',
'SELECT ', @columns, ' FROM mytable ',
'WHERE id = 1;'
)
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
Questo è come io sto facendo in PHP:
$sql='';
$sql.="SET @columns := (
SELECT
GROUP_CONCAT(column_name)
FROM information_schema.columns
WHERE table_schema = 'test'
AND table_name = 'mytable'
AND column_key <> 'PRI'
);";
$sql.="SET @sql := (
SELECT CONCAT(
'INSERT INTO mytable (', @columns, ') ',
'SELECT ', @columns, ' FROM mytable ',
'WHERE id = 1;'
)
);";
$sql.="PREPARE stmt FROM @sql;
EXECUTE stmt;";
$result = mysql_query($sql, $this->connection);
Che cosa sto facendo di sbagliato?
vedere l'errore sto ottenendo ::
Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET @sql := (
SELECT CONCAT(
'INSERT INTO mytable(', @colu' at line 9
Per quanto ne so, l'estensione deprecata di mysql _... non supporta l'esecuzione di più istruzioni. Dovrai eseguirli uno per uno. –
Perché non stai usando [PDO] (http://php.net/manual/en/book.pdo.php)? – Bojangles
@ ÁlvaroG.Vicario ho provato in questo modo anche lo stesso risultato .... cosa fare..la frase si rompe quando raggiunge @columns ... !! – tradebel123