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


Posted in PHP onFebruary 13, 2019

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

一 代码

sql语句:

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>用户注册</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("请输入用户昵称!");
     form.nc.select();
     return(false);
    }
  if(form.pwd.value==""){
     alert("请输入注册密码!");
     form.pwd.select();
     return(false);
    }
     if(form.email.value==""){
     alert("请输入E-mail地址!");
     form.email.select();
     return(false);
    }
    if(form.address.value==""){
     alert("请输入家庭地址!");
     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">用户注册</div></td>
   </tr>
   <tr>
    <td width="150" height="25" bgcolor="#FFFFFF"><div align="center">用户昵称:</div></td>
    <td width="347" bgcolor="#FFFFFF"> <input type="text" name="nc" class="inputcss" size="25"></td>
   </tr>
   <tr>
    <td height="25" bgcolor="#FFFFFF"><div align="center">注册密码:</div></td>
    <td height="25" bgcolor="#FFFFFF"> <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"> <input type="text" name="email" class="inputcss" size="25"></td>
   </tr>
   <tr>
    <td height="25" bgcolor="#FFFFFF"><div align="center">家庭住址:</div></td>
    <td height="25" bgcolor="#FFFFFF"> <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="注册" class="buttoncss">  <input type="reset" value="重写" 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 />
    版权所有 吉林省**科技有限公司! 未经授权禁止复制或建立镜像!<br />
   Copyright © , All Rights Reserved! <br />
   <br />
   建议您在大于1024*768的分辨率下使用 </div></td>
 </tr>
</table>
<?php
 if($_POST['submit']!=""){
     $dbms='mysql'; //数据库类型 ,对于开发者来说,使用不同的数据库,只要改这个,不用记住那么多的函数
    $host='localhost'; //数据库主机名
    $dbName='db_database15'; //使用的数据库
    $user='root'; //数据库连接用户名
    $pass='root'; //对应的密码
    $dsn="$dbms:host=$host;dbname=$dbName";
    try {
      $pdo = new PDO($dsn, $user, $pass); //初始化一个PDO对象,就是创建了数据库连接对象$pdo
        $pdo->query("set names utf8"); //设置数据库编码格式
        $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 "数据添加成功!";
        }else{
            echo "数据添加失败!";
        }
    } 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>

二 运行结果

数据添加成功!

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
在php中使用sockets:从新闻组中获取文章
Oct 09 PHP
php set_time_limit(0) 设置程序执行时间的函数
May 26 PHP
WordPress判断用户是否登录的代码
Mar 17 PHP
PHP curl模拟浏览器采集阿里巴巴的实现代码
Apr 20 PHP
PHP动态编译出现Cannot find autoconf的解决方法
Nov 05 PHP
php通过array_merge()函数合并关联和非关联数组的方法
Mar 18 PHP
PHP内核探索之解释器的执行过程
Dec 22 PHP
ThinkPHP使用Ueditor的方法详解
May 20 PHP
PHP设计模式之工厂模式与单例模式
Sep 28 PHP
PHP实现非阻塞模式的方法分析
Jul 26 PHP
ThinkPHP3.2.3框架Memcache缓存使用方法实例总结
Apr 15 PHP
PHP迭代器和生成器用法实例分析
Sep 28 PHP
在PHP中输出JS语句以及乱码问题的解决方案
Feb 13 #PHP
PHP实现通过文本文件统计页面访问量功能示例
Feb 13 #PHP
Windows上php5.6操作mongodb数据库示例【配置、连接、获取实例】
Feb 13 #PHP
PHP封装的简单连接MongoDB类示例
Feb 13 #PHP
总结PHP中初始化空数组的最佳方法
Feb 13 #PHP
PHP convert_cyr_string()函数讲解
Feb 13 #PHP
php如何比较两个浮点数是否相等详解
Feb 12 #PHP
You might like
PHP+Mysql+jQuery实现动态展示信息
2011/10/08 PHP
php通过获取头信息判断图片类型的方法
2015/06/26 PHP
PHP基于方差和标准差计算学生成绩的稳定性示例
2017/07/04 PHP
PHP并发查询MySQL的实例代码
2017/08/09 PHP
一个支付页面DEMO附截图
2014/07/22 Javascript
jquery mobile 移动web(5)
2015/12/20 Javascript
React实现点击删除列表中对应项
2017/01/10 Javascript
Javascript中this关键字指向问题的测试与详解
2017/08/11 Javascript
使用html+js+css 实现页面轮播图效果(实例讲解)
2017/09/21 Javascript
NodeJS服务器实现gzip压缩的示例代码
2018/10/12 NodeJs
jQuery事件blur()方法的使用实例讲解
2019/03/30 jQuery
卸载vue2.0并升级vue_cli3.0的实例讲解
2020/02/16 Javascript
vue treeselect获取当前选中项的label实例
2020/08/31 Javascript
[44:50]2018DOTA2亚洲邀请赛 4.1 小组赛 A组 TNC vs VG
2018/04/02 DOTA
使用Python判断IP地址合法性的方法实例
2014/03/13 Python
关于你不想知道的所有Python3 unicode特性
2014/11/28 Python
Python的批量远程管理和部署工具Fabric用法实例
2015/01/23 Python
举例区分Python中的浅复制与深复制
2015/07/02 Python
Python利用前序和中序遍历结果重建二叉树的方法
2016/04/27 Python
Python中shutil模块的常用文件操作函数用法示例
2016/07/05 Python
Python利用flask sqlalchemy实现分页效果
2020/08/02 Python
Python开发SQLite3数据库相关操作详解【连接,查询,插入,更新,删除,关闭等】
2017/07/27 Python
numpy向空的二维数组中添加元素的方法
2018/11/01 Python
Python流程控制 while循环实现解析
2019/09/02 Python
Python3 实现爬取网站下所有URL方式
2020/01/16 Python
使用Python爬虫库requests发送请求、传递URL参数、定制headers
2020/01/25 Python
如何在Python 游戏中模拟引力
2020/03/27 Python
CSS3自定义滚动条样式的示例代码
2017/08/21 HTML / CSS
HTML5 本地存储之如果没有数据库究竟会怎样
2013/04/25 HTML / CSS
Nike意大利官网:Nike.com IT
2020/01/19 全球购物
专科文秘应届生求职信
2013/11/18 职场文书
工作鉴定评语
2014/05/04 职场文书
承诺书格式
2014/06/03 职场文书
上课睡觉检讨书300字
2014/11/18 职场文书
2019秋季运动会口号
2019/06/25 职场文书
Axios取消重复请求的方法实例详解
2021/06/15 Javascript