Report error while performing UPDATE with SQL

Asked

Viewed 923 times

5

I have a project, in which the administrator can change registered data of tables, but if the query is executed, but no change occurs, I would like to return a message or error, here’s what I have so far:

$altera = "UPDATE z SET y= a WHERE x= x";
$select = mysql_query($altera);
if($select){
   mensagem de sucesso
}else{
   mensagem de erro
}

The variables and things like that are not important, what matters is the if there at the end, which, I think, if the variable occurs, will do what is inside the brackets, if it doesn’t occur, will show error message, but this doesn’t work, and I don’t know who else to turn to.

  • What exactly is going wrong ? Everything seems to be in order here.

  • As I said just below @Edilson, as long as I type true fields, it returns a success message, although there is no such value.

  • @Edilson, in relation to his answer, when taking the variable $select and using it in this way, it becomes Boolean, however, the mysql_affected_rows works as integer, and the $altera, comes as string, by chance would have to solve this problem?

  • 1

    It was my error writing the syntax. I will now edit the answer.

  • 1

    Okay, now it should work. The indenter passed to this function should be the connection link mysql_connect. If not specified, mysql_affected_rows automatically use the last connection to open.

2 answers

4

I use it this way with Firebird:

if ($query){
    echo "Tudo OK";
}

if (ibase_errmsg()){
    echo "Erro técnico: ".ibase_errmsg();
}

You can do with the function mysql_error().

mysql_error - Returns the text of the previous Mysql operation error message

Example with Mysql with your problem:

  $altera = "UPDATE z SET y= a WHERE x= x";
  $select = mysql_query($altera);

  if($select){
      // tratativa caso for bem-sucedido
  } 

  if (mysql_error()){
      // tratativa caso for mal-sucedido
  }

Observing:

Mysql functions that start with mysql_* are obsolete and will be discontinued from PHP 7, consider using mysqli_*.

References:

http://php.net/manual/en/function.mysql-error.php

http://php.net/manual/en/function.ibase-errmsg.php

  • 2

    I would suggest using only if + Else with mysql_errno() for checking, instead of if($select) and another redundant if. mysql_error() looks better to display the message on the screen optionally.

  • 1

    Thanks for the @Bacco suggestion, I’ll remind you the next time you need to treat an Exception in php

2


This can be done using the function mysql_affected_rows which returns the number of lines actually affected in the last query using the UPDATE.

$altera = "UPDATE z SET y= a WHERE x= x";
$select = mysql_query($altera);
   if($select){
      if(mysql_affected_rows() > 0){
        print "foram atualizadas (". mysql_affected_rows() .") linhas";
      } else {
        print "consulta efetuada, mas nenhuma linha modificada";
      }
   }else{ 
      die("Erro: " . mysql_error());
   }

Regardless of what you want, both the if($select) or if(!$select) as a mysql_errno() would have the same impact if it were to verify whether such a query was executed, and if($select) checks if the returned value has been executed (true) or not (false), mysql_errno() returns the error number of the last operation, if it has failed, the same way it does the mysql_error() but this returns the error as a numerical value.

For the compatibility problem with even lower versions, there is the function mysql_numrows() (also discontinued).

Warning This Extension was deprecated in PHP 5.5.0, and it was Removed in PHP 7.0.0. Instead, the Mysqli or Pdo_mysql Extension should be used. See also Mysql: Choosing an API guide and Related FAQ for more information.

In the documentation in Portuguese this warning is omitted, or why I do not know, but it practically says that functions like mysql_* are obsolete, that is, the use of these functions is discouraged, from the PHP >= 5.5.0 and were completely removed from the PHP >= 7.0.0.

Alternatively, there are the following extensions:

Some references:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.