PHP實(shí)現(xiàn)PDO操作mysql存儲(chǔ)過(guò)程示例
本文實(shí)例講述了PHP實(shí)現(xiàn)PDO操作mysql存儲(chǔ)過(guò)程。分享給大家供大家參考,具體如下:
一 代碼
sql語(yǔ)句:
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>用戶注冊(cè)</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("請(qǐng)輸入用戶昵稱!"); form.nc.select(); return(false); } if(form.pwd.value==""){ alert("請(qǐng)輸入注冊(cè)密碼!"); form.pwd.select(); return(false); } if(form.email.value==""){ alert("請(qǐng)輸入E-mail地址!"); form.email.select(); return(false); } if(form.address.value==""){ alert("請(qǐng)輸入家庭地址!"); 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">用戶注冊(cè)</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">注冊(cè)密碼:</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="注冊(cè)" 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 /> 版權(quán)所有 吉林省**科技有限公司! 未經(jīng)授權(quán)禁止復(fù)制或建立鏡像!<br /> Copyright © , All Rights Reserved! <br /> <br /> 建議您在大于1024*768的分辨率下使用 </div></td> </tr> </table> <?php if($_POST['submit']!=""){ $dbms='mysql'; //數(shù)據(jù)庫(kù)類型 ,對(duì)于開(kāi)發(fā)者來(lái)說(shuō),使用不同的數(shù)據(jù)庫(kù),只要改這個(gè),不用記住那么多的函數(shù) $host='localhost'; //數(shù)據(jù)庫(kù)主機(jī)名 $dbName='db_database15'; //使用的數(shù)據(jù)庫(kù) $user='root'; //數(shù)據(jù)庫(kù)連接用戶名 $pass='root'; //對(duì)應(yīng)的密碼 $dsn="$dbms:host=$host;dbname=$dbName"; try { $pdo = new PDO($dsn, $user, $pass); //初始化一個(gè)PDO對(duì)象,就是創(chuàng)建了數(shù)據(jù)庫(kù)連接對(duì)象$pdo $pdo->query("set names utf8"); //設(shè)置數(shù)據(jù)庫(kù)編碼格式 $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>
二 運(yùn)行結(jié)果
數(shù)據(jù)添加成功!
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫(kù)技巧總結(jié)》、《php+Oracle數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫(kù)操作技巧大全》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP使用PDO實(shí)現(xiàn)mysql防注入功能詳解
- PHP連接MySQL數(shù)據(jù)庫(kù)的三種方式實(shí)例分析【mysql、mysqli、pdo】
- PHP使用PDO、mysqli擴(kuò)展實(shí)現(xiàn)與數(shù)據(jù)庫(kù)交互操作詳解
- PHP使用PDO創(chuàng)建MySQL數(shù)據(jù)庫(kù)、表及插入多條數(shù)據(jù)操作示例
- php使用mysqli和pdo擴(kuò)展,測(cè)試對(duì)比mysql數(shù)據(jù)庫(kù)的執(zhí)行效率完整示例
- php使用mysqli和pdo擴(kuò)展,測(cè)試對(duì)比連接mysql數(shù)據(jù)庫(kù)的效率完整示例
- PHP使用PDO操作sqlite數(shù)據(jù)庫(kù)應(yīng)用案例
- PHP基于PDO擴(kuò)展操作mysql數(shù)據(jù)庫(kù)示例
- PHP基于pdo的數(shù)據(jù)庫(kù)操作類【可支持mysql、sqlserver及oracle】
- PHP如何初始化PDO及原始SQL語(yǔ)句操作
相關(guān)文章
實(shí)現(xiàn)dedecms全站URL靜態(tài)化改造的代碼
實(shí)現(xiàn)dedecms全站URL靜態(tài)化改造的代碼...2007-03-03php daddslashes()和 saddslashes()有哪些區(qū)別分析
在Discuze 開(kāi)源項(xiàng)目中會(huì)經(jīng)常用到 saddslashes 函數(shù),這里簡(jiǎn)單介紹下,方便需要的朋友2012-10-10PHP實(shí)現(xiàn)負(fù)載均衡下的session共用功能
這篇文章主要介紹了PHP實(shí)現(xiàn)負(fù)載均衡下的session共用功能,結(jié)合實(shí)例形式分析了php基于memcache的session共享相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-04-04PHP如何獲取Cookie并實(shí)現(xiàn)模擬登錄
這篇文章主要介紹了PHP如何獲取Cookie并實(shí)現(xiàn)模擬登錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07通過(guò)PHP自帶的服務(wù)器來(lái)查看正則匹配結(jié)果的方法
這篇文章主要介紹了通過(guò)PHP自帶的服務(wù)器來(lái)查看正則匹配結(jié)果的方法,通過(guò)這樣的方式可以在搬上服務(wù)器之前在工作環(huán)境上完成很多輕量級(jí)的試驗(yàn),需要的朋友可以參考下2015-12-12PHP+Ajax簡(jiǎn)單get驗(yàn)證操作示例
這篇文章主要介紹了PHP+Ajax簡(jiǎn)單get驗(yàn)證操作,涉及php結(jié)合ajax無(wú)刷新提交驗(yàn)證相關(guān)操作技巧,需要的朋友可以參考下2019-03-03