PHP实现PDO操作mysql存储过程示例

这篇文章主要介绍了PHP实现PDO操作mysql存储过程,结合具体实例形式分析了php使用pdo操作mysql存储过程实现用户注册功能相关技巧,需要的朋友可以参考下.

本文实例讲述了PHP实现PDO操作mysql存储过程,分享给大家供大家参考,具体如下:

一 代码

sql语句:

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

index.php:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>用户注册</title>
  7. <link rel="stylesheet" type="text/css" href="style.css" rel="external nofollow" >
  8. </head>
  9. <script language="javascript">
  10. function chkinput(form){
  11. if(form.nc.value==""){
  12. alert("请输入用户昵称!");
  13. form.nc.select();
  14. return(false);
  15. }
  16. if(form.pwd.value==""){
  17. alert("请输入注册密码!");
  18. form.pwd.select();
  19. return(false);
  20. }
  21. if(form.email.value==""){
  22. alert("请输入E-mail地址!");
  23. form.email.select();
  24. return(false);
  25. }
  26. if(form.address.value==""){
  27. alert("请输入家庭地址!");
  28. form.address.select();
  29. return(false);
  30. }
  31. return(true);
  32. }
  33. </script>
  34. <body>
  35. <table width="200" align="center" cellpadding="0" cellspacing="0">
  36. <tr>
  37. <td><img src="images/banner.gif" width="500" height="65" /></td>
  38. </tr>
  39. </table>
  40. <table width="500" height="10" align="center" cellpadding="0" cellspacing="0">
  41. <tr>
  42. <td></td>
  43. </tr>
  44. </table>
  45. <table width="500" align="center" cellpadding="0" cellspacing="0">
  46. <tr>
  47. <td bgcolor="#1170FF"><table width="500" height="157" align="center" cellpadding="0" cellspacing="1">
  48. <form name="form1" method="post" action="index.php" onsubmit="return chkinput(this)">
  49. <tr>
  50. <td height="25" colspan="2" bgcolor="#B5D3FF"><div align="center">用户注册</div></td>
  51. </tr>
  52. <tr>
  53. <td width="150" height="25" bgcolor="#FFFFFF"><div align="center">用户昵称:</div></td>
  54. <td width="347" bgcolor="#FFFFFF">&nbsp;<input type="text" name="nc" class="inputcss" size="25"></td>
  55. </tr>
  56. <tr>
  57. <td height="25" bgcolor="#FFFFFF"><div align="center">注册密码:</div></td>
  58. <td height="25" bgcolor="#FFFFFF">&nbsp;<input type="password" name="pwd" class="inputcss" size="25"></td>
  59. </tr>
  60. <tr>
  61. <td height="25" bgcolor="#FFFFFF"><div align="center">E-mail:</div></td>
  62. <td height="25" bgcolor="#FFFFFF">&nbsp;<input type="text" name="email" class="inputcss" size="25"></td>
  63. </tr>
  64. <tr>
  65. <td height="25" bgcolor="#FFFFFF"><div align="center">家庭住址:</div></td>
  66. <td height="25" bgcolor="#FFFFFF">&nbsp;<input type="text" name="address" class="inputcss" size="25"></td>
  67. </tr>
  68. <tr>
  69. <td height="25" colspan="2" bgcolor="#FFFFFF"><div align="center"><input type="submit" name="submit" value="注册" class="buttoncss">&nbsp;&nbsp;<input type="reset" value="重写" class="buttoncss"></div></td>
  70. </tr>
  71. </form>
  72. </table></td>
  73. </tr>
  74. </table>
  75. <table width="600" height="80" align="center" cellpadding="0" cellspacing="0">
  76. <tr>
  77. <td><div align="center"><br />
  78. 版权所有&nbsp;吉林省**科技有限公司! 未经授权禁止复制或建立镜像!<br />
  79. Copyright ©&nbsp;, All Rights Reserved! <br />
  80. <br />
  81. 建议您在大于1024*768的分辨率下使用 </div></td>
  82. </tr>
  83. </table>
  84. <?php
  85. if($_POST['submit']!=""){
  86. $dbms='mysql'; //数据库类型 ,对于开发者来说,使用不同的数据库,只要改这个,不用记住那么多的函数
  87. $host='localhost'; //数据库主机名
  88. $dbName='db_database15'; //使用的数据库
  89. $user='root'; //数据库连接用户名
  90. $pass='root'; //对应的密码
  91. $dsn="$dbms:host=$host;dbname=$dbName";
  92. try {
  93. $pdo = new PDO($dsn, $user, $pass); //初始化一个PDO对象,就是创建了数据库连接对象$pdo
  94. $pdo->query("set names utf8"); //设置数据库编码格式
  95. $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  96. $nc=$_POST['nc'];
  97. $pwd=md5($_POST['pwd']);
  98. $email=$_POST['email'];
  99. $address=$_POST['address'];
  100. $query="call pro_reg('$nc','$pwd','$email','$address')";
  101. $result=$pdo->prepare($query);
  102. if($result->execute()){
  103. echo "数据添加成功!";
  104. }else{
  105. echo "数据添加失败!";
  106. }
  107. } catch (PDOException $e) {
  108. echo 'PDO Exception Caught.';
  109. echo 'Error with the database:<br/>';
  110. echo 'SQL Query: '.$query;
  111. echo '<pre>';
  112. echo "Error: " . $e->getMessage(). "<br/>";
  113. echo "Code: " . $e->getCode(). "<br/>";
  114. echo "File: " . $e->getFile(). "<br/>";
  115. echo "Line: " . $e->getLine(). "<br/>";
  116. echo "Trace: " . $e->getTraceAsString(). "<br/>";
  117. echo '</pre>';
  118. }
  119. }
  120. ?>
  121. </body>
  122. </html>

二 运行结果

数据添加成功!