how to receive a connection failure if the update is not successful?

0

Let's say I have an update button on the bank.

$query = mysql_query("UPDATE $tabela set algo='$algo' WHERE id=1);

if ($query) {
    echo "sucesso";
} else {
    echo 'falha';
}

To check if the query was successfully registered in the database (if there was no connection error (eg the connection crashed) or if there was an error in the update) I do the above procedure?

    
asked by anonymous 09.12.2015 / 19:42

1 answer

0

With mysql_affected_rows you can see how many records have been affected. Home If it is zero, the update did not work.

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Não foi possível conectar: ' . mysql_error());
}
mysql_select_db('mydb');

/* Atualiza os registros */
mysql_query("UPDATE mytable SET used=1 WHERE id < 10");
printf ("Registros atualizados: %d\n", mysql_affected_rows());
mysql_query("COMMIT");
    
09.12.2015 / 19:50