Monday, July 9, 2007

Using stored Procedures MYSQL-PHP

Usually MySQLi API fails when 2 subsiquent calls are made to a stored procedure from PHP and after much googling around and playing with code i finally found fix ...

Create procedure


delimiter $$
create procedure getLoginIds()
begin
select loginid from user order by id desc limit 100;
end$$


delimiter $$
create procedure getLatestSchedules()
begin
select * from tschedules where active=1 order by id desc limit 100;
end$$







  1. <?php





  2. $con=new mysqli('dev.host.com','uname','pwd','db');



  3. if(mysqli_connect_errno()){

  4.  die("Failed to connect");

  5. }





  6. $sql='call getLoginIds()';



  7. $result=$con->query($sql);

  8. if(!$result){

  9.  die("failed");

  10. }

  11. //$result=$result1->fetch_assoc();



  12. while($row=$result->fetch_row()){

  13.   echo $row[0]."<br>";

  14.   

  15.   

  16.  }

  17.  $result->free();

  18.  

  19.  cleanSP($con);



  20. //calling second sp

  21. $sql='call getLatestSchedules()';



  22. $result=$con->query($sql);

  23. if(mysqli_connect_errno()){

  24.  die("Failed to connect");

  25. }





  26. while($row=$result->fetch_row()){

  27.  echo $row[0]."<br>";



  28.  

  29. }

  30. cleanSP($con);



  31. //cleans the result set in the connection

  32. function cleanSP($con){

  33.  while($con->next_result()){

  34.  }

  35. }

  36. ?>


No comments: