PHP Implementation PDO Operation mysql Stored Procedure Sample

  • 2021-11-24 01:09:46
  • OfStack

This article illustrates the PHP implementation PDO operation mysql stored procedure. Share it for your reference, as follows:

1 code

sql statement:


create procedure pro_reg (in nc varchar(80), in pwd varchar(80), in email varchar(80),in address varchar(50))
begin
insert into tb_reg (name, pwd ,email ,address) values (nc, pwd, email, address);
end;

index. php:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> User registration </title>
<link rel="stylesheet" type="text/css" href="style.css" rel="external nofollow" >
</head>
<script language="javascript">
 function chkinput(form){
  if(form.nc.value==""){
     alert(" Please enter a user nickname !");
     form.nc.select();
     return(false);
    }
  if(form.pwd.value==""){
     alert(" Please enter your registration password !");
     form.pwd.select();
     return(false);
    }
     if(form.email.value==""){
     alert(" Please enter E-mail Address !");
     form.email.select();
     return(false);
    }
    if(form.address.value==""){
     alert(" Please enter your home address !");
     form.address.select();
     return(false);
    }
  return(true);
 }
</script>
<body>
<table width="200" border="0" align="center" cellpadding="0" cellspacing="0">
 <tr>
  <td><img src="images/banner.gif" width="500" height="65" /></td>
 </tr>
</table>
<table width="500" height="10" border="0" align="center" cellpadding="0" cellspacing="0">
 <tr>
  <td></td>
 </tr>
</table>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
 <tr>
  <td bgcolor="#1170FF"><table width="500" height="157" border="0" align="center" cellpadding="0" cellspacing="1">
   <form name="form1" method="post" action="index.php" onsubmit="return chkinput(this)">
     <tr>
    <td height="25" colspan="2" bgcolor="#B5D3FF"><div align="center"> User registration </div></td>
   </tr>
   <tr>
    <td width="150" height="25" bgcolor="#FFFFFF"><div align="center"> User nickname: </div></td>
    <td width="347" bgcolor="#FFFFFF">&nbsp;<input type="text" name="nc" class="inputcss" size="25"></td>
   </tr>
   <tr>
    <td height="25" bgcolor="#FFFFFF"><div align="center"> Registration password: </div></td>
    <td height="25" bgcolor="#FFFFFF">&nbsp;<input type="password" name="pwd" class="inputcss" size="25"></td>
   </tr>
   <tr>
    <td height="25" bgcolor="#FFFFFF"><div align="center">E-mail : </div></td>
    <td height="25" bgcolor="#FFFFFF">&nbsp;<input type="text" name="email" class="inputcss" size="25"></td>
   </tr>
   <tr>
    <td height="25" bgcolor="#FFFFFF"><div align="center"> Home address: </div></td>
    <td height="25" bgcolor="#FFFFFF">&nbsp;<input type="text" name="address" class="inputcss" size="25"></td>
   </tr>
   <tr>
    <td height="25" colspan="2" bgcolor="#FFFFFF"><div align="center"><input type="submit" name="submit" value=" Registration " class="buttoncss">&nbsp;&nbsp;<input type="reset" value=" Rewrite " class="buttoncss"></div></td>
   </tr>
     </form>
  </table></td>
 </tr>
</table>
<table width="600" height="80" border="0" align="center" cellpadding="0" cellspacing="0">
 <tr>
  <td><div align="center"><br />
     Copyright reserved &nbsp; Jilin Province ** Technology co., ltd !  Unauthorized prohibition of copying or mirroring !<br />
   Copyright &copy;&nbsp;, All Rights Reserved! <br />
   <br />
    It is recommended that you set the 1024*768 Use at the resolution of  </div></td>
 </tr>
</table>
<?php
 if($_POST['submit']!=""){
     $dbms='mysql'; // Database type  , For developers to use different databases, just change this and don't have to remember so many functions 
    $host='localhost'; // Database Hostname 
    $dbName='db_database15'; // Database used 
    $user='root'; // Database connection user name 
    $pass='root'; // Corresponding password 
    $dsn="$dbms:host=$host;dbname=$dbName";
    try {
      $pdo = new PDO($dsn, $user, $pass); // Initialization 1 A PDO Object, that is, the database connection object is created $pdo
        $pdo->query("set names utf8"); // Setting database encoding format 
        $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
         $nc=$_POST['nc'];
      $pwd=md5($_POST['pwd']);
      $email=$_POST['email'];
      $address=$_POST['address'];
        $query="call pro_reg('$nc','$pwd','$email','$address')";
        $result=$pdo->prepare($query);
        if($result->execute()){
            echo " Data added successfully! ";
        }else{
            echo " Data addition failed! ";
        }
    } catch (PDOException $e) {
      echo 'PDO Exception Caught.';
        echo 'Error with the database:<br/>';
        echo 'SQL Query: '.$query;
        echo '<pre>';
      echo "Error: " . $e->getMessage(). "<br/>";
        echo "Code: " . $e->getCode(). "<br/>";
        echo "File: " . $e->getFile(). "<br/>";
        echo "Line: " . $e->getLine(). "<br/>";
        echo "Trace: " . $e->getTraceAsString(). "<br/>";
        echo '</pre>';
    }
 }
?>
</body>
</html>

2 Running results

Data added successfully!

For more readers interested in PHP related contents, please check the topics on this site: "Summary of PHP Database Operation Skills Based on pdo", "Summary of php+Oracle Database Programming Skills", "Encyclopedia of PHP+MongoDB Database Operation Skills", "Introduction to php Object-Oriented Programming", "Introduction to php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: