想让客户公司做网站的话语,网站建设找什么工作,吉林市建设厅网站,推广普通话的广告语一、连接数据库基本函数
mysqli_connect(); 作用#xff1a;创建数据库连接#xff0c;打开一个新的mysql的连接。传参顺序#xff1a;数据库地址、数据库账号、数据库密码
?phpecho mysqli_connect(localhost,root,root)
?
/*结果#xff1a;F…
一、连接数据库基本函数
mysqli_connect(); 作用创建数据库连接打开一个新的mysql的连接。传参顺序数据库地址、数据库账号、数据库密码
?phpecho mysqli_connect(localhost,root,root)
?
/*结果Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in C:\Users\Administrator\Desktop\网络安全\php\project_01\php_connect.php:2 Stack trace: #0 {main} thrown in C:\Users\Administrator\Desktop\网络安全\php\project_01\php_connect.php on line 2*/寻错 翻译错误为致命错误未捕获错误调用未定义的函数mysqli_connect出现错误原因未配置php.ini 解决方法 参考https://blog.csdn.net/sxudong2010/article/details/83277285 重新测试连接
?php
$connmysqli_connect(localhost,root,root);
if($conn){echook;
}else{echoerror;
}
//phpinfo();
? 效果图 mysqli_select_db 作用选择需要的数据库传参顺序mysqli对象数据库名
mysqli_select_db($conn, php); #选择名为php的数据库mysqli_query 作用可以对数据库中的表进行增删改查传参顺序mysqli对象SQL语句
# 修改编码
mysqli_query($conn, SET NAMES utf8);
# $GLOBALS是将conn变量赋予全局属性
# 查询user表中userName字段为abc的数据
# 返回值为mysqli_result对象
mysqli_query($GLOBALS[conn], select * from user where userName abc)
mysqli_fetch_row和mysqli_fetch_all() 区别 row()只返回一条数据适合有条件的查询如返回Array时为一维数组all返回查询到的所有数据使用列表展示功能如返回Array时为二维数组 作用接收查询数据并并以多种形式返回传参顺序mysqli_result对象
# 参数为mysqli_query()函数返回的 mysqli_result对象
# 返回结果为Array
echo mysqli_fetch_all( mysqli_query($GLOBALS[conn], select * from user ))
echo mysqli_fetch_row( mysqli_query($GLOBALS[conn], select * from user where userName123)) 二、案例实现 1、功能说明
该案例主要功能为用户登录和修改密码功能
2、涉及知识
php变量作用域的范围php对于Session的存储、修改和销毁php对于字符串Base64编码和解码的应用php对于字符串判空、去空、去特殊值的处理php对于Mysql的如何连接、选择数据库、增删改查等功能
3、页面分布
index.php登录页index.php个人中心页register.php注册页db.php对于数据库一系列操作utril.php工具方法style.css页面通用css样式
4、db.php
?php
# 创建一次新的mysql连接
$conn mysqli_connect(localhost, root, root) or dir(数据库连接失败);
# var_dump($conn);
#连接上名为php的数据库
mysqli_select_db($conn, php);
# 修改编码
mysqli_query($conn, SET NAMES utf8);
/*** 判断用户是否存在* $name用户名* return:返回存在的数据数组* */
function isQueryUserName($name){return mysqli_fetch_row( mysqli_query($GLOBALS[conn], select * from user where userName $name));
}
/** 更新密码* $userName用户名* $password 密码* */
function updatePassword($userName,$password){$password base64_encode($password);return mysqli_query($GLOBALS[conn], update user set password $password where userName $userName);
}?5、util.php
?php
/*判断数据是否为空* */
function isEmpty($value){return !empty($value)?true:false;
}
/** 去除多余格式* trim去除左右两边空格* stripslashes去除反斜杠* htmlspecialchars把预定义的字符转换为 HTML 实体* 预定义的字符是和号成为 amp; 双引号成为 quot; 单引号成为 小于成为 lt; 大于成为 gt;** */
function outFormat($value){$value trim($value);$value stripslashes($value);$value htmlspecialchars($value);return $value;}// echo base64_encode(admin); // 编码# echo base64_decode(dmFyaW4); // 解码
/** 匹配8位由大写或小写或数字* */
function passwordReg($password){return preg_match(/^[a-zA-Z0-9]{8}$/, $password);
} 6、style.css
*{padding:0px;margin:0px;
}
.box{margin: 100px auto;background: linear-gradient(135deg, #d3e4f5, #0088a9, #00c9a7, #92d5c6, #ebf5ee) repeat-x ;width:600px;height:500px;border:1px solid #f5f5f5;border-radius: 15px;box-shadow: 10px 10px 10px #f5f5f5;color: white;
}
h2{text-align: center;margin: 20px 0px;
}
label{margin: 20px 0px;display: inline-table;
}
form{text-align: center;
}
input{color: black;
}7、login.php
代码
?php# 开启sessionsession_start();
# 包含
include ./db.php;
include ./util.php;
$userName$password;
# 检测
if(isset($_POST[loginSubmit])){$userName outFormat( $_POST[userName]);$password outFormat($_POST[password]);# 判空if(isEmpty($userName) isEmpty($password)){# 查询$userInfoArray isQueryUserName($userName);# 比对数据if($userInfoArray!null $userInfoArray[1] $userName base64_decode($userInfoArray[2]) $password){echo scriptalert(登录成功);/script;# 存session$_SESSION[userName]$userName;// echo $_SESSION[userName];echo scriptwindow.location.hrefindex.php;/script;}else{echo scriptalert(用户信息错误请重试);/script;}}else{echo scriptalert(请将信息填写完整);/script;}
}?!DOCTYPE htmlhtml langenheadmeta charsetUTF-8titleLogin/titlelink href./style.css typetext/css relstylesheet/headbodydiv classboxh2登录/h2form action./login.php methodPOSTlabel 用户名input typetext nameuserName value?php echo $userName?/labelbrlabel 密nbsp;nbsp; nbsp;码input typepassword namepassword value?php echo $password?/labelbrinput typesubmit nameloginSubmit value登录 stylewidth:60px;height: 30px;color: black;margin-top: 100pxbrp stylemargin-top: 20px;color: black;font-size: 14px 请a href# stylecolor: green注册/a/p/form/div/body/html效果 8、index.php
代码
?phpsession_start();include ./db.php;include ./util.php;$userName$password$confirmPassword;$userName$_SESSION[userName];if($userNamenull){echo scriptwindow.location.hreflogin.php;/script;}$password base64_decode(isQueryUserName($userName)[2]);if(isset($_POST[updateSubmit])){$password $_POST[password];$confirmPassword $_POST[confirmPassword];echo $password;echo $confirmPassword;if(isEmpty($password) isEmpty($confirmPassword)){if(passwordReg($password) $password $confirmPassword){if (updatePassword($userName, $password)true) {echo scriptalert(用户密码更新成功 )/script;session_destroy();echo scriptwindow.location.hreflogin.php;/script;}}else{echo scriptalert(密码格式错误请重试)/script;}}else{echo scriptalert(请将信息填写完整)/script;}}
?
!DOCTYPE html
html langen
headmeta charsetUTF-8title个人中心/titlelink href./style.css typetext/css relstylesheet
/head
body
div classboxh2个人中心/h2form action# methodpostlabel 欢迎您span stylefont-weight: bold ?php echo $userName?/span/labelbrlabel 密nbsp;nbsp; nbsp;码input typepassword namepassword value?php echo $password?/labelbrlabel 确认密码input typepassword nameconfirmPassword value?php echo $confirmPassword ?/labelbrinput typesubmit nameupdateSubmit value修改 stylewidth:60px;height: 30px;color: black;margin-top: 100pxbr/form
/div/body
/html效果 9、register.php
注注册页面并为实现注册功能大致功能代码与index.php页面类似。
!DOCTYPE html
html langen
headmeta charsetUTF-8titleLogin/titlelink href./style.css typetext/css relstylesheet/head
body
div classboxh2注册/h2form action# methodpostlabel 用户名input typetext nameuserName value用户名/labelbrlabel 密nbsp;nbsp; nbsp;码input typepassword namepassword value密码/labelbrinput typesubmit nameloginSubmit value登录 stylewidth:60px;height: 30px;color: black;margin-top: 100pxbrp stylemargin-top: 20px;color: black;font-size: 14px 请a href# stylecolor: green注册/a/p/form
/div/body
/html
文章转载自: http://www.morning.tbqdm.cn.gov.cn.tbqdm.cn http://www.morning.rknhd.cn.gov.cn.rknhd.cn http://www.morning.mnkhk.cn.gov.cn.mnkhk.cn http://www.morning.rnmmh.cn.gov.cn.rnmmh.cn http://www.morning.hfxks.cn.gov.cn.hfxks.cn http://www.morning.ltspm.cn.gov.cn.ltspm.cn http://www.morning.zlnf.cn.gov.cn.zlnf.cn http://www.morning.snbq.cn.gov.cn.snbq.cn http://www.morning.kcsx.cn.gov.cn.kcsx.cn http://www.morning.gcrlb.cn.gov.cn.gcrlb.cn http://www.morning.fndmk.cn.gov.cn.fndmk.cn http://www.morning.hmjasw.com.gov.cn.hmjasw.com http://www.morning.yxbrn.cn.gov.cn.yxbrn.cn http://www.morning.stcds.cn.gov.cn.stcds.cn http://www.morning.tgxrm.cn.gov.cn.tgxrm.cn http://www.morning.bflwj.cn.gov.cn.bflwj.cn http://www.morning.cfccp.cn.gov.cn.cfccp.cn http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.zylrk.cn.gov.cn.zylrk.cn http://www.morning.cbynh.cn.gov.cn.cbynh.cn http://www.morning.rhsg.cn.gov.cn.rhsg.cn http://www.morning.fmqw.cn.gov.cn.fmqw.cn http://www.morning.bpmtx.cn.gov.cn.bpmtx.cn http://www.morning.zrrgx.cn.gov.cn.zrrgx.cn http://www.morning.dqwkm.cn.gov.cn.dqwkm.cn http://www.morning.sphft.cn.gov.cn.sphft.cn http://www.morning.xrpwk.cn.gov.cn.xrpwk.cn http://www.morning.flchj.cn.gov.cn.flchj.cn http://www.morning.tnfyj.cn.gov.cn.tnfyj.cn http://www.morning.hslgq.cn.gov.cn.hslgq.cn http://www.morning.zwhtr.cn.gov.cn.zwhtr.cn http://www.morning.cjmmn.cn.gov.cn.cjmmn.cn http://www.morning.rqbkc.cn.gov.cn.rqbkc.cn http://www.morning.tqxtx.cn.gov.cn.tqxtx.cn http://www.morning.qfbzj.cn.gov.cn.qfbzj.cn http://www.morning.khpgd.cn.gov.cn.khpgd.cn http://www.morning.nfcxq.cn.gov.cn.nfcxq.cn http://www.morning.wjfzp.cn.gov.cn.wjfzp.cn http://www.morning.mjats.com.gov.cn.mjats.com http://www.morning.ybhjs.cn.gov.cn.ybhjs.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.dygqq.cn.gov.cn.dygqq.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.kdbcx.cn.gov.cn.kdbcx.cn http://www.morning.lgpzq.cn.gov.cn.lgpzq.cn http://www.morning.cgtrz.cn.gov.cn.cgtrz.cn http://www.morning.mrxgm.cn.gov.cn.mrxgm.cn http://www.morning.twgzq.cn.gov.cn.twgzq.cn http://www.morning.xxknq.cn.gov.cn.xxknq.cn http://www.morning.xtqld.cn.gov.cn.xtqld.cn http://www.morning.nwfpl.cn.gov.cn.nwfpl.cn http://www.morning.xxwfq.cn.gov.cn.xxwfq.cn http://www.morning.mpnff.cn.gov.cn.mpnff.cn http://www.morning.dlhxj.cn.gov.cn.dlhxj.cn http://www.morning.rltw.cn.gov.cn.rltw.cn http://www.morning.qmfhh.cn.gov.cn.qmfhh.cn http://www.morning.mzhh.cn.gov.cn.mzhh.cn http://www.morning.fqmbt.cn.gov.cn.fqmbt.cn http://www.morning.qtbnm.cn.gov.cn.qtbnm.cn http://www.morning.qgfhr.cn.gov.cn.qgfhr.cn http://www.morning.hgfxg.cn.gov.cn.hgfxg.cn http://www.morning.bpptt.cn.gov.cn.bpptt.cn http://www.morning.sqhlx.cn.gov.cn.sqhlx.cn http://www.morning.rbhcx.cn.gov.cn.rbhcx.cn http://www.morning.slqzb.cn.gov.cn.slqzb.cn http://www.morning.ctfh.cn.gov.cn.ctfh.cn http://www.morning.haibuli.com.gov.cn.haibuli.com http://www.morning.bfcrp.cn.gov.cn.bfcrp.cn http://www.morning.rkdhh.cn.gov.cn.rkdhh.cn http://www.morning.trtdg.cn.gov.cn.trtdg.cn http://www.morning.nxkyr.cn.gov.cn.nxkyr.cn http://www.morning.cplym.cn.gov.cn.cplym.cn http://www.morning.jjhng.cn.gov.cn.jjhng.cn http://www.morning.mywmb.cn.gov.cn.mywmb.cn http://www.morning.kpzrf.cn.gov.cn.kpzrf.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.bynf.cn.gov.cn.bynf.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.yybcx.cn.gov.cn.yybcx.cn http://www.morning.qzdxy.cn.gov.cn.qzdxy.cn