我需要执行数据库上的存储过程,才能单击Html按钮!
存储过程的名称: DeleteRow
<form action="">
<input type="button" value="Check" onclick="">
</form>
如何使用php执行?有可能吗?
** SQL查询:**
CALL `DeleteRow`();
答案 0 :(得分:0)
您可以尝试以下操作:
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 );
}
?>
答案 1 :(得分:-1)
假设您已存储过程类似
DELIMITER $$
CREATE PROCEDURE getDetails()
BEGIN
SELECT * FROM tablename;
END$$
现在您可以在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
*/
}
单击时,可以实现Ajax以使其正常运行。您可以关注PHP Ajax Example