If you are using persistent connections (odbc_pconnect rather than odbc_connect) the next script that reuses the connection will inherit your changes to autocommit.
odbc_autocommit
(PHP 4, PHP 5)
odbc_autocommit — Activa el comportamiento automático de envío
Descripción
Activa el comportamiento automático de envío.
Por defecto, el auto envío está activado en una conexión. Deshabilitar el auto envío es equivalente a iniciar una transacción.
Parámetros
- connection_id
-
El conector identificador ODBC, ver odbc_connect() para más información.
- OnOff
-
Si OnOff es TRUE, el auto envío está habilitado, si es FALSE el auto envío está deshabilitado.
Valores devueltos
Sin el parámetro OnOff, está función devuelve el estado de auto envío para connection_id. Se devuelve un valor distinto de cero si el auto envío está activado, 0 si está desactivado, o FALSE si se produjo un error.
Si está establecido OnOff, esta función devolverá TRUE si se tuvo éxito y FALSE si falló.
Hi (i'm belgian then sorry for my english).
I think you can do more simple to check the errors :
$conn = odbc_connect($odbc,$user,$password)
or die($error);
odbc_autocommit($conn, FALSE);
odbc_exec($conn, $query1);
odbc_exec($conn, $query2);
if (!odbc_error())
odbc_commit($conn);
else
odbc_rollback($conn);
odbc_close($conn);
I'm not sure it's better to use odbc_error() than
odbc_error($conn). It seems to be the same result.
using autocommit functionality to rollback transactions in a "workspace"
<?php
$conn = odbc_connection("$DSN","USER","PASS");
//turns off autocommit
odbc_autocommit($conn,FALSE);
$query1 = "some SQL";
$query2 = "some more SQL";
$ErrorCount = 0;
//if odbc_exec fails it will increment the $ErrorCount
$result = odbc_exec($conn,$query1) or $ErrorCount++;
$result2 = odbc_exec($conn,$query2) or $ErrorCount++;
//checking for errors, commit if none, rollback else
if ($Errorcount == 0){
odbc_commit($conn);
echo "transaction successful";
}
else{
odbc_rollback($conn);
echo "there were errors processing the transaction.
No changes were made to the database";
}
odbc_close($conn);
?>
Cheers,
Ron
It seems that example made by andrea dot galli at acotel dot com works exactly the contrary.
It sets autocommit OFF and NOT ON like it's written inside note!
If a transaction is started (autocommit disabled) while there is an active result id on the connection, odbc_autocommit will post a warning (Cannot set autocommit). Use odbc_free_result to clear the result id's or start the transaction before you execute the SQL.
Example: set autocommit on
<?php
$Link_ID = odbc_connect("DSN", "user", "pass");
$Return = odbc_autocommit($Link_ID, FALSE);
?>
When used in a odbc_fetch loop your selected resultset is lost and loop ends.
