I want to insert records in a table from another table. The first record to insert is the record that has the sum of the amounts of the records in the source table. (Position) And the rest of the records to insert are those that appear in the source table (Subscriptions).
Only the first query is executed, inserting the corresponding record.
I have this:
$BD = new ConexionDB();
$libro = 10
$asiento = ObtenerMaxAsiento();
//---------- CARGO
$sql = "SET @id = (SELECT max(id) from tabla);
SET @total = (SELECT SUM(importe) from temporal WHERE cruza_cod = 'X');
INSERT INTO tabla (id, libro, asiento, correlativo, periodo, importe)
VALUES (@id:=@id+1, $libro, $asiento, 1, $periodo, @total)";
$sth = $BD->query($sql);
//funcion obtiene maximo id
$id = ObtenerMax();
//---------- ABONO
$sql = "INSERT INTO tabla (id, libro, asiento, correlativo, periodo, importe)
SELECT @id:=@id+1 as id, $libro, $asiento, @corr:=@corr+1 as corr, periodo, importe
FROM (SELECT @id:= $id) r, (SELECT @corr:=1) s, temporal WHERE cruza_cod='X'";
$sth = $BD->query($sql);
When you run, it shows me:
Fatal error error: Uncaught exception 'PDOException' with message 'SQLSTATE [HY000]: General error: 2014 Can not execute queries while other unbuffered queries are active. Consider using PDOStatement :: fetchAll (). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO :: MYSQL_ATTR_USE_BUFFERED_QUERY attribute
My connection to the MySQL database is with PHP / PDO.
class ConexionDB extends PDO {
public function __construct () {
try {
parent:: __construct('mysql:host='.DB_HOST.';dbname='.DB_DATA.';charset=utf8', DB_USER, DB_KEY);
parent:: setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $ex) {
die ('La Base de Datos no existe');
}
}
function __destruct(){
}
}
I run it in Xampp 1.8.2 What is the problem? DS