本文實例講述了PHP實現(xiàn)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">nbsp;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">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">家庭住址:/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="注冊" class="buttoncss">nbsp;nbsp;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 />
版權所有nbsp;吉林省**科技有限公司! 未經(jīng)授權禁止復制或建立鏡像!br />
Copyright copy;nbsp;, All Rights Reserved! br />
br />
建議您在大于1024*768的分辨率下使用 /div>/td>
/tr>
/table>
?php
if($_POST['submit']!=""){
$dbms='mysql'; //數(shù)據(jù)庫類型 ,對于開發(fā)者來說,使用不同的數(shù)據(jù)庫,只要改這個,不用記住那么多的函數(shù)
$host='localhost'; //數(shù)據(jù)庫主機名
$dbName='db_database15'; //使用的數(shù)據(jù)庫
$user='root'; //數(shù)據(jù)庫連接用戶名
$pass='root'; //對應的密碼
$dsn="$dbms:host=$host;dbname=$dbName";
try {
$pdo = new PDO($dsn, $user, $pass); //初始化一個PDO對象,就是創(chuàng)建了數(shù)據(jù)庫連接對象$pdo
$pdo->query("set names utf8"); //設置數(shù)據(jù)庫編碼格式
$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 "數(shù)據(jù)添加成功!";
}else{
echo "數(shù)據(jù)添加失??!";
}
} 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>
二 運行結果
數(shù)據(jù)添加成功!
更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫技巧總結》、《php+Oracle數(shù)據(jù)庫程序設計技巧總結》、《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:- PHP使用PDO實現(xiàn)mysql防注入功能詳解
- PHP連接MySQL數(shù)據(jù)庫的三種方式實例分析【mysql、mysqli、pdo】
- PHP使用PDO、mysqli擴展實現(xiàn)與數(shù)據(jù)庫交互操作詳解
- PHP使用PDO創(chuàng)建MySQL數(shù)據(jù)庫、表及插入多條數(shù)據(jù)操作示例
- php使用mysqli和pdo擴展,測試對比mysql數(shù)據(jù)庫的執(zhí)行效率完整示例
- php使用mysqli和pdo擴展,測試對比連接mysql數(shù)據(jù)庫的效率完整示例
- PHP使用PDO操作sqlite數(shù)據(jù)庫應用案例
- PHP基于PDO擴展操作mysql數(shù)據(jù)庫示例
- PHP基于pdo的數(shù)據(jù)庫操作類【可支持mysql、sqlserver及oracle】
- PHP如何初始化PDO及原始SQL語句操作