Quiero insertar registros en una tabla desde otra tabla. El primer registro a insertar es el registro que tiene la suma de los importes de los registros de la tabla origen. (Cargo) Y el resto de registros a insertar son los que aparecen en la tabla origen (Abonos).
Solamente se ejecuta el primer query, insertando el registro correspondiente.
Tengo esto:
$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);
Al ejecutar me muestra:
error de Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2014 Cannot 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
Mi conexión a la base de datos MySQL es con 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(){
}
}
Lo ejecuto en Xampp 1.8.2 Cuál es el problema?DS