-1

I need to execute stored procedure which is on database, to on click Html button!!

name of stored procedure: DeleteRow

    <form action="">
    <input type="button" value="Check" onclick="">
    </form>

How I can execute using php?? Is it possible?

** SQL query:**

CALL `DeleteRow`();
anonymous
  • 119
  • 9
  • 1
    Either submit a form or make an Ajax request which then runs some PHP on the server which calls the procedure. Standard web application structure – ADyson Apr 13 '19 at 18:11

2 Answers2

2

You can try something like this:

HTML:

<input type='button' value='Call Procedure' id='BtnId'>

jQuery:

$(document).ready(function(){

    $("#BtnId").on( 'click', function(){

        $.ajax({
            type : "POST",
            url : "callsp.php",
            success : function(text){
                alert(text);
            }
        });

        return false;
    }); 

});

PHP: (callsp.php)

<?php

// connect to database
$connection = mysqli_connect("hostname", "username", "password", "db", "port");

// run the query
$result = mysqli_query($connection, "CALL DeleteRow") or die("Query Failed: " . mysqli_error());

// loop the result set
while( $row = mysqli_fetch_array($result) ) {
  print_r( $row );
}

?>
Naveed
  • 40,370
  • 32
  • 94
  • 130
0

Let's say you have stored procedure something like

DELIMITER $$

CREATE PROCEDURE getDetails()
BEGIN
   SELECT * FROM tablename;
END$$

Now you can execute this in the following way in PHP

try {
  $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
  // execute the stored procedure
  $sql = 'CALL getDetails()';
  // call the stored procedure
  $q = $pdo->query($sql);
  $q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
  die("Error occurred:" . $e->getMessage());
}

while ($r = $q->fetch()){
  /*
  * Do something
  */
}

On click, you can implement Ajax to make it work. You can follow PHP Ajax Example

miken32
  • 39,644
  • 15
  • 91
  • 133
Rakesh Jakhar
  • 6,298
  • 2
  • 9
  • 20