How can I empty several tables with a single sequence?

0

Hello, I have a button by which it says "delete everything", and I would like that when I clicked empty several tables, I was informed that this: $sql = "truncate tabla1,tabla2,tabla3,etc"; is not possible, so I saw an answer that I did not understand that said it could be done:

truncate table1;
go
truncate table2;
go
truncate table3;
...

And if it is true how it would be? Would it be like this?

$sql ="
truncate table1;
go
truncate table2;
go
truncate table3;
";

and if you can not, there is another solution?

    
asked by cat_12 06.07.2018 в 18:35
source

2 answers

0

You can do it with an arrangement

$truncate = array( 'tabla1', 'tabla2', 'tabla3' );

foreach( $truncate as $table ){
  $sql = "truncate " . $table;
  mysqli_query( $tuConexion, $sql );
}

I used mysqli_query , but you can put it to how you are using it

    
answered by 06.07.2018 в 18:42
0

I already found another solution, since I did not find a solution to empty several tables with a sequence in MySQL, what I did was put all the AJAX calls in an AJAX. like this:

        var fecha = $("#form_fecha").serialize();
    $.ajax({
      type:"POST",
      url:"eliminar_todo_vacaciones_navidad.php",
      data:fecha,
      success:function(x){
        if (x==1) {
          var fecha = $("#form_fecha").serialize();
          $.ajax({
            type:"POST",
            url:"eliminar_todo_inicio_curso1.php",
            data:fecha,
            success:function(y){
              if (y==1) {
                var fecha = $("#form_fecha").serialize();
                $.ajax({
                  type:"POST",
                  url:"eliminar_todo_vacaciones_pascua.php",
                  data:fecha,
                  success:function(z){
                    if (z==1) {
                      var fecha = $("#form_fecha").serialize();
                      $.ajax({
                        type:"POST",
                        url:"eliminar_todo_dias_festivos.php",
                        data:fecha,
                        success:function(){

                        }
                      });
                    }
                  }
                });
              }
            }
          });
        }
      }
    }).done(function(){
        $("#correcto_eliminar").animate({
          top:"2%"
        },500);
        $("#correcto_eliminar").delay(1000);
        $("#correcto_eliminar").animate({
          top:"-15%"
        },500);
    }).fail(function(){
        $("#error_eliminar").animate({
          top:"2%"
        },500);
        $("#error_eliminar").delay(1000);
        $("#error_eliminar").animate({
          top:"-15%"
        },500); 
    });

Logically. And it worked to me also logically if any AJAX request fails me it will show me what I have put in fail () and if all are correct, it will do what I do in the done ().

Thanks for your interest.

    
answered by 06.07.2018 в 21:49