PHP全栈学习笔记11


连接MySQL
mysql_connect(servername,username,password);
面向对象:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// 创建连接
$conn = new mysqli($servername, $username, $password);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
?>
面向过程:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "连接成功";
?>
PDO:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
echo "连接成功";
}
catch(PDOException $e){
echo $e->getMessage();
}
?>
连接在脚本执行完成后会自动关闭
//面向对象
$conn->close();
//面向过程
mysqli_close($conn);
//PDO
$conn = null;
创建数据库
CREATE DATABASE database_name
INSERT INTO table_name
VALUES (value1, value2,....)
SELECT column_name(s) FROM table_name
order by关键词用于对于记录集中的数据进行排序
SELECT column_name(s)
FROM table_name
ORDER BY column_name
SELECT column_name(s)
FROM table_name
ORDER BY column_name DESC
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
DELETE FROM table_name
WHERE some_column = some_value
连接到ODBC
odbc_connect() 函数用于连接到 ODBC 数据源
$conn=odbc_connect('northwind','','');
$sql="SELECT * FROM customers";
$rs=odbc_exec($conn,$sql);
odbc_fetch_row($rs)
odbc_fetch_row() 函数用于从结果集中返回记录
odbc_result() 函数用于从记录中读取字段
odbc_close() 函数用于关闭 ODBC 连接
array() 创建数组
array_change_key_case() 返回均为大写或小写的数组
array_chunk() 把一个数组分割为新的数组块
array_column() 返回输入数组中某个单一列的值
array_combine() 通过合并两个数组来创建新的数组
array_count_values() 用于统计数组中所有值出现的次数
array_diff() 比较数组,返回两个数组的差集
array_diff_assoc() 比较数组,返回两个数组的差集
array_diff_key() 比较数组,返回两个数组的差集
array_diff_uassoc() 比较数组,返回两个数组的差集
array_diff_ukey() 比较数组,返回两个数组的差集
array_key_exists() 检查指定的键名是否存在于数组中
array_map() 将用户自定义函数作用到给定数组的每个值上,返回新的值
array_merge() 把一个或多个数组合并为一个数组
array_multisort() 对多个数组或多维数组进行排序
array_pad() 将指定数量的带有指定值的元素插入到数组中
array_pop() 删除数组中的最后一个元素
array_product() 计算数组中所有值的乘积
array_push() 将一个或多个元素插入数组的末尾
array_rand() 从数组中随机选出一个或多个元素,返回键名
array_replace() 使用后面数组的值替换第一个数组的值
array_reverse() 将原数组中的元素顺序翻转,创建新的数组并返回
array_search() 在数组中搜索给定的值,如果成功则返回相应的键名
array_shift() 删除数组中的第一个元素,并返回被删除元素的值
array_slice() 返回数组中的选定部分
array_splice() 把数组中的指定元素去掉并用其它值取代
array_sum() 返回数组中所有值的和
array_unique() 删除数组中重复的值
array_unshift() 在数组开头插入一个或多个元素
array_values() 返回数组中所有的值
arsort() 对关联数组按照键值进行降序排序
asort() 对关联数组按照键值进行升序排序
count() 返回数组中元素的数目
each() 返回数组中当前的键/值对
current() 返回数组中的当前元素
end() 将数组的内部指针指向最后一个元素
extract() 从数组中将变量导入到当前的符号表
in_array() 检查数组中是否存在指定的值
key() 从关联数组中取得键名
krsort() 对关联数组按照键名降序排序
ksort() 对关联数组按照键名升序排序
list() 把数组中的值赋给一些数组变量
next() 将数组中的内部指针向后移动一位
prev() 将数组的内部指针倒回一位
range() 创建一个包含指定范围的元素的数组
reset() 将数组的内部指针指向第一个元素
rsort() 对数值数组进行降序排序
sort() 对数值数组进行升序排序
uasort() 对数组中的键值进行排序
uksort() 对数组中的键名进行排序
usort() 使用用户自定义的比较函数对数组进行排序
PHP加密函数

单向加密功能
string crypt(string str[, string salt]);

使用md5()函数进行加密
string md5 ( string str [, bool raw_output] );
使用sha1()函数进行加密
string sha1 ( string str [, bool raw_output] )
1、Mcrypt扩展库
2、Mhash扩展库
在php.ini文件中找到“;extension=php_mcrypt.dll”和“;extension=php_mhash.dll”
<!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=gb2312" />
<title>使用md5()和sha1()函数进行加密</title>
</head>
<body>
<div align="center">
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td height="30" colspan="2" align="center" valign="middle" scope="col"><?php echo 'md5()和shal()函数的对比效果'; ?></td>
</tr>
<tr>
<td width="200" height="30" align="right" valign="middle"><?php echo '使用md5()函数加密字符串PHPER:' ?></td>
<td width="200" height="30" align="center" valign="middle"><?php echo md5('PHPER'); ?></td>
</tr>
<tr>
<td width="200" height="30" align="right" valign="middle"><?php echo '使用shal()函数加密字符串PHPER:'; ?></td>
<td width="200" height="30" align="center" valign="middle"><?php echo sha1('PHPER'); ?></td>
</tr>
</table>
</div>
</body>
</html>
<!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=gb2312" />
<title>Mcrypt库支持的加密算法与加密模式</title>
</head>
<body>
<?php
$en_dir = mcrypt_list_algorithms();
echo "Mcrypt支持的算法有:";
foreach($en_dir as $en_value){
echo $en_value." ";
}
?>
<?php
$mo_dir = mcrypt_list_modes();
echo "<p>Mcrypt支持的加密模式有:";
foreach($mo_dir as $mo_value){
echo $mo_value." ";
}
?>
</body>
</html>
<!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=gb2312" />
<title>使用mcrypt库加密解密</title>
></head>
<body>
<?php
$str = "被加密的内容:相见时难别亦难 东风无力百花残";
$key = "key:111";
$cipher = MCRYPT_DES;
$modes = MCRYPT_MODE_ECB;
//初始化向量
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$modes),MCRYPT_RAND);
echo "加密前:".$str."<p>";
//加密:
$str_encrypt = mcrypt_encrypt($cipher,$key,$str,$modes,$iv);
echo "加密后:".$str_encrypt." <p>";
$str_decrypt = mcrypt_decrypt($cipher,$key,$str_encrypt,$modes,$iv);
echo "还原:".$str_decrypt;
?>
</body>
</html>
<!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=gb2312" />
<title>Mhash库支持的加密算法</title>
</head>
<body>
<?php
$num = mhash_count(); //函数返回最大的hash id
echo "Mhash库支持的算法有:";
for($i = 0; $i <= $num; $i++){
echo $i."=>".mhash_get_hash_name($i)." "; //输出每一个hash id 的名称
}
?>
</body>
</html>
<!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=gb2312" />
<title>使用mhash库生成校验码</title>
</head>
<body>
<?php
$filename = '08.txt';
$str = file_get_contents($filename);
$hash = 2;
$password = '111';
$salt = '1234';
$key = mhash_keygen_s2k(1,$password,$salt,10);
$str_mhash = bin2hex(mhash($hash,$str,$key));
echo "文件08.txt的校验码是:".$str_mhash;
?>
</body>
</html>
登录:
<script language="javascript">
function chk(form){
if(form.name.value == '' || form.pwd.value == ''){
alert('请输入信息');
return false;
}
}
</script>
<table border="1" cellpadding="0" cellspacing="0" align="center">
<form id="lg" name="lg" method="post" action="index_chk.php" onSubmit="return chk(lg)">
<tr>
<td colspan="2" height="25" align="center" valign="middle">用户登录</td>
</tr>
<tr>
<td width="75" height="25" align="right" valign="middle">用户名:</td>
<td height="25" align="left" valign="middle"><input name="name" type="text" size="20" maxlength="20"></td>
</tr>
<tr>
<td height="25" align="right" valign="middle">密码:</td>
<td height="25" align="left" valign="middle"><input name="pwd" type="password" size="20" maxlength="20"></td>
</tr>
<tr>
<td height="25" align="right">登录方式:</td>
<td align="left">
<select name="method">
<option value="crypt">crypt加密</option>
<option value="md5">md5加密</option>
</select>
</td>
</tr>
<tr>
<td height="25" colspan="2" align="center" valign="middle"><input type="submit" value="登陆"> <a href="reg.php">注册</a></td>
</tr>
</form>
</table>
<?php
$name = $_POST['name'];
$pwd = $_POST['pwd'];
$meth = $_POST['method'];
$key = 'tomorrow';
if($name != '' and $pwd != ''){
$conn = mysql_connect("localhost","root","root") or die("数据库链接错误".mysql_error());
mysql_select_db("db_database15",$conn) or die("数据库访问错误".mysql_error());
mysql_query("set names gb2312");
if($meth == "crypt"){
$pwd = crypt(trim($pwd),$key);
}else{
$pwd = md5($pwd);
}
$sql = "select * from tb_user where user = '".$name."' and password = '".$pwd."'";
$rst = mysql_query($sql,$conn);
if(mysql_num_rows($rst) > 0){
echo "<font color='red'>用户登陆成功</font> <a href=index.php>返回</a>";
}else{
echo "<script>alert('密码或帐号输入错误');history.go(-1);</script>";
}
}
?>
<script language="javascript">
function rg(form){
if(form.name.value == '' || form.pwd.value == ''){
alert('请填写信息');
return false;
}
if(form.pwd.value != form.pwd2.value){
alert("两次密码不同");
return false;
}
}
</script>
<table border="1" cellpadding="0" cellspacing="0" align="center">
<form id="reg" name="reg" method="post" action="reg_chk.php" onSubmit="return rg(reg)">
<tr>
<td colspan="2" align="center" valign="middle" height="25">用户注册</td>
</tr>
<tr>
<td width="75" height="25" align="right">用户名:</td>
<td align="left"><input id="name" name="name" type="text" size="20" /></td>
</tr>
<tr>
<td height="25" align="right">密码:</td>
<td align="left"><input id="pwd" name="pwd" type="password" size="20" /></td>
</tr>
<tr>
<td height="25" align="right">确认密码:</td>
<td align="left"><input id="pwd2" name="pwd2" type="password" size="20" /></td>
</tr>
<tr>
<td height="25" align="right">加密方式:</td>
<td align="left">
<select name="method">
<option value="crypt">crypt加密</option>
<option value="md5">md5加密</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" height="25" align="center">
<input type="submit" value="注册" />
<a href="index.php">登录</a>
</td>
</tr>
</form>
</table>
<?php
$name = $_POST['name'];
$pwd = $_POST['pwd'];
$meth = $_POST['method'];
$key = 'tomorrow';
if($name != '' and $pwd != ''){
$conn = mysql_connect("localhost","root","root") or die("数据库链接错误".mysql_error());
mysql_select_db("db_database15",$conn) or die("数据库访问错误".mysql_error());
mysql_query("set names gb2312");
if($meth == "crypt"){
$pwd = crypt(trim($pwd),$key);
}else{
$pwd = md5($pwd);
}
$sql = "insert into tb_user(user,password) values('".$name."','".$pwd."')";
$rst = mysql_query($sql,$conn);
if($rst){
echo "<script>alert('添加成功,请登录');location='index.php';</script>";
}else{
echo "<script>alert('添加失败');history.go(-1);</script>";
}
}
?>
结言
好了,欢迎在留言区留言,与大家分享你的经验和心得。
感谢你学习今天的内容,如果你觉得这篇文章对你有帮助的话,也欢迎把它分享给更多的朋友,感谢。
感谢!承蒙关照!您真诚的赞赏是我前进的最大动力!
PHP全栈学习笔记11的更多相关文章
- PHP全栈学习笔记7
图形图像处理技术,gd库的强大支持,PHP的图像可以是PHP的强项,PHP图形化类库,jpgraph是一款非常好用的强大的图形处理工具. 在PHP中加载GD库 gd官方网址下载: http://www ...
- PHP全栈学习笔记18
php基础知识,JavaScript,jQuery,ajax基础知识 linux基础知识,mysql数据库的基础与优化 程序设计,PHP框架基础知识,算法,逻辑思维,高并发 PHP基础知识 引用变量, ...
- python全栈学习笔记(一)网络基础之网络协议篇
阅读目录 一.操作系统基础 二.网络通信原理 2.1 互联网的本质就是一系列的网络协议 2.2 osi七层协议 2.3 tcp/ip五层模型讲解 2.3.1 物理层 2.3.2 数据链路层 2.3.3 ...
- PHP全栈学习笔记29
前言 这一章主要讲一讲PHP的背景,优势,PHP的环境搭建,书写和调式简单的PHP代码,如何解决简单的PHP错误等. 目录结构 PHP简介 PHP是面向对象,指令式编程,设计者是 拉斯姆斯·勒多夫 出 ...
- PHP全栈学习笔记13
php与ajax技术 web2.0的到来,ajax逐渐成为主流,什么是ajax,ajax的开发模式,优点,使用技术.(ajax概述,ajax使用的技术,需要注意的 问题,在PHP应用ajax技术的应用 ...
- PHP全栈学习笔记12
php简介,php历史,php后端工程师职业前景,php技术方向,php后端工程师职业体系介绍. php是世界上使用最广泛的web开发语言,是超文本预处理器,是一种通用的开源脚本语言,语法吸收了c语言 ...
- PHP全栈学习笔记17
phpmyadmin教程 管理页进入phpmyadmin 打开C:\wamp\apps\phpmyadmin3.5.1下的配置文件:config.inc 修改密码 创建与修改数据库.数据表 字段类型 ...
- PHP全栈学习笔记16
<?php $fileName = "php大师.test.php"; //补充程序,显示文件名(不包括扩展名) $start = strrpos($fileName, &q ...
- PHP全栈学习笔记15
PHP标记风格 PHP一共支持4种标记风格 <?php echo "这是XML风格的标记"; ?> 脚本风格 <script language="php ...
随机推荐
- SSH概念及常用操作汇总
工作有一段时间了,经常用SSH登录远程机器,但对原理一直不是很了解,所以查阅了一些资料,写个小结. 一. SSH是什么? SSH的全称是Secure Shell, 是一种“用来在不安全的网络上安全地运 ...
- java基础小项目练习之1----3天做出飞机大战
Shoot射击游戏第一天一. 关键问题(理论):1. 简述FlyingObject.Enemy.Award.Airplane.Bee.Bullet.Hero之间的继承与实现关系2. 简述Hero类构造 ...
- 理解Go Interface
理解Go Interface 1 概述 Go语言中的接口很特别,而且提供了难以置信的一系列灵活性和抽象性.接口是一个自定义类型,它是一组方法的集合,要有方法为接口类型就被认为是该接口.从定义上来看,接 ...
- C++中的4种类型转换方式
类型转换有c风格的,当然还有c++风格的.c风格的转换的格式很简单(TYPE)EXPRESSION,但是c风格的类型转换有不少的缺点,有的时候用c风格的转换是不合适的,因为它可以在任意类型之间转换,比 ...
- 【源码分析】Canal之Binlog的寻找过程
binlog的寻找过程可能的场景如下: instance第一次启动 发生数据库主备切换 canal server HA情况下的切换 所以这个过程是能够保证binlog不丢失的关键点. 本文从源码的角度 ...
- BZOJ_1345_[Baltic2007]序列问题Sequence_单调栈
BZOJ_1345_[Baltic2007]序列问题Sequence_单调栈 Description 对于一个给定的序列a1,…,an,我们对它进行一个操作reduce(i),该操作将数列中的元素ai ...
- rand ----MATLAB (经典)
最近一直在学习matlab,我相信有一些同学已经发现,最近更新的关于matlab的内容比较多, 希望能够帮助到未来的小学弟学妹们! 永远爱你们的 ----新宝宝 rand 均匀分布的随机数全页折叠 语 ...
- ASP.NET Core的实时库: SignalR简介及使用
大纲 本系列会分为2-3篇文章. 第一篇介绍了SignalR的预备知识和原理 本文介绍SignalR以及ASP.NET Core里使用SignalR. 本文的内容: 介绍SignalR 在ASP.NE ...
- python输出彩色字体
Python终端如何输出彩色字体 实现过程: 终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关. 转义序列是以ESC开头,即用\033来完成(ES ...
- 『Zap Möbius反演』
Zap Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a ,y<=b,并且gcd(x,y)=d.作为F ...