How to execute 2 or more stored procedures with mysqli in php?

3

I have 2 procedures stored in the same file, the connection is made correctly because if I delete any of the 2 procedures, the other one shows information but if I leave both, only the first one is executed.

I have the file conectar.php with the following code:

class Clase_Conecta{
     var $_Server = 'localhost';
     var $_User = 'root';
     var $_Password = '';
     var $_Bd = 'bd1';

     function Fn_getConnect(){
        if (!($conexion = new mysqli($this -> _Server, $this -> _User, $this -> _Password, $this -> _Bd))){
           echo "Error Conectando la base de Datos";
           exit();
        }else{
          echo "conexion exitosa";
        }
        return $conexion;
     }
  }

And my file principal.php with the following:

include ("conectar.php");
$Fn = new Clase_Conecta();
$Cn = $Fn -> Fn_getConnect();

$sql1 = "call sp1()";
$r = query($sql1);

while ($rP = $r -> fetch_array()){
            //Imprime tabla
       }

//Hasta aquí funciona bien pero si quiero ejecutar otro procedimiento almacenado no lo hace
//solo ejecuta el de arriba (el primero)

$sql2 = "call sp2()";
$r2 = query($sql2);

 while ($rP2 = $r2 -> fetch_array()){
            //Imprime tabla
     }

I imagine that I am misunderstanding the concepts but I have already read and investigated and it is not entirely clear to me, please if someone is so kind to orient me that I am doing wrong or that I am omitting

    
asked by El Cóndor 30.12.2016 в 19:58
source

1 answer

2

You see, it does not work for you because you are missing the ; in the sql code.

It is an error in the sql syntax.

The code should look like this:

include ("conectar.php");
$Fn = new Clase_Conecta();
$Cn = $Fn -> Fn_getConnect();

$sql1 = "call sp1();";
$r = query($sql1);

while ($rP = $r -> fetch_array()){
            //Imprime tabla
       }
$r->close();
$Cn->next_result();
//Hasta aquí funciona bien pero si quiero ejecutar otro procedimiento almacenado no lo hace
//solo ejecuta el de arriba (el primero)

$sql2 = "call sp2();";
$r2 = query($sql2);

 while ($rP2 = $r2 -> fetch_array()){
            //Imprime tabla
     }
 $r2->close();
 $Cn->next_result();

answered by 30.12.2016 / 20:02
source