导读:今天首席CTO笔记来给各位分享关于php怎么注册链接数据库的相关内容,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
首先得到提交的数据
链接数据库,查询数据库,查询username 和pwd
提交的username 和 pwd 跟数据库查询的username 和pwd做对比,
都相等那就是登陆成功
?php
mysql_connect('localhost','root','123');
mysql_select_db('lx');
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES utf8");
//数据库lx 表user 字段id username pwd
//用md5加密,可以自己试试
if(isset($_POST['user'])$_POST['tijiao'] == 'success'){
$query = mysql_query("select pwd from user where username = '".$_POST['user']."'");
$num = mysql_num_rows($query);
if($num 0 ){
while($info = mysql_fetch_array($query)){
if($info['pwd'] == md5($_POST['pwd'])){
echo '登陆成功';
}else{
echo '登陆失败';
}
}
}else{
echo '登陆失败';
}
}
?
form action="" method="get"/
table border="0" cellspacing="0" cellpadding="0"
tr
td class="fieldKey" width="30%"用户名:/td
td class="fieldValue" width="100%"input type="text" name="user" //td
/tr
trtd height="10"/td/tr
tr
td class="fieldKey"密码:/td
td class="fieldValue"input type="password" name="pwd" //td
/tr
/table
input type="hidden" name="tijiao" value="success" /
input type="submit" value="登陆"/
/form
常规方式
常规方式就是按部就班的读取文件了。其余的话和上述方案一致。
// 读取配置文件内容
$handle = fopen("filepath", "r"); $content = fread($handle, filesize("filepath"));123
PHP解析XML
上述两种读取文件,其实都是为了PHP解析XML来做准备的。关于PHP解析XML的方式的博客有很多。方式也有很多,像simplexml,XMLReader,DOM啦等等。但是对于比较小型的xml配置文件,simplexml就足够了。
配置文件
?xml version="1.0" encoding="UTF-8" ?mysql
!-- 为防止出现意外,请按照此标准顺序书写.其实也无所谓了 --
hostlocalhost/host
userroot/user
password123456/password
dbtest/db
port3306/port/mysql12345678910
解析
?php/**
* 作为解析XML配置文件必备工具
*/class XMLUtil {
public static $dbconfigpath = "./db.config.xml"; public static function getDBConfiguration() {
$dbconfig = array (); try { // 读取配置文件内容
$handle = fopen(self::$dbconfigpath, "r"); $content = fread($handle, filesize(self::$dbconfigpath)); // 获取xml文档根节点,进而获取相关的数据库信息
$mysql = simplexml_load_string($content); // 将获取到的xml节点信息赋值给关联数组,方便接下来的方法调用
$dbconfig['host'] = $mysql-host; $dbconfig['user'] = $mysql-user; $dbconfig['password'] = $mysql-password; $dbconfig['db'] = $mysql-db; $dbconfig['port'] = $mysql-port; // 将配置信息以关联数组的形式返回
return $dbconfig;
} catch ( Exception $e ) { throw new RuntimeException ( "mark读取数据库配置文件信息出错!/markbr /" );
} return $dbconfig;
}
}1234567891011121314151617181920212223242526272829
数据库连接池
对于PHP程序而言,优化永无止境。而数据库连接池就在一定程度上起到了优化的作用。其使得对用户的每一个请求而言,无需每次都像数据库申请链接资源。而是通过已存在的数据库连接池中的链接来返回,从时间上,效率上,都是一个大大的提升。
于是,这里简单的模拟了一下数据库连接池的实现。核心在于维护一个“池”。
从池子中取,用毕,归还给池子。
?php/**x
* PHP中的数据库 工具类设计
* 郭璞
* 2016年12月23日
*
**/class DbHelper { private $dbconfig; private $dbpool; public $poolsize; public function __construct($poolsize = 20) { if (! file_exists ( "./utils.php" )) { throw new RuntimeException ( "markutils.php文件丢失,无法进行配置文件的初始化操作!/markbr /" );
}else {
require './utils.php';
} // 初始化 配置文件信息
$this-dbconfig = XMLUtil::getDBConfiguration (); // 准备好数据库连接池“伪队列”
$this-poolsize = $poolsize;
$this-dbpool = array (); for($index = 1; $index = $this-poolsize; $index ++) {
$conn = mysqli_connect ( $this-dbconfig ['host'], $this-dbconfig ['user'], $this-dbconfig ['password'], $this-dbconfig ['db'] ) or die ( "mark连接数据库失败!/markbr /" );
array_push ( $this-dbpool, $conn );
}
} /**
* 从数据库连接池中获取一个数据库链接资源
*
* @throws ErrorException
* @return mixed
*/
public function getConn() { if (count ( $this-dbpool ) = 0) { throw new ErrorException ( "mark数据库连接池中已无链接资源,请稍后重试!/mark" );
} else { return array_pop ( $this-dbpool );
}
} /**
* 将用完的数据库链接资源放回到数据库连接池
*
* @param unknown $conn
* @throws ErrorException
*/
public function release($conn) { if (count ( $this-dbpool ) = $this-poolsize) { throw new ErrorException ( "mark数据库连接池已满/markbr /" );
} else {
array_push ( $this-dbpool, $conn );
}
}
}
先会写html的表单
然后会写php接收表单。
然后会连接数据库,查找数据库对应字段。
判断下
匹配跳转到相应页面,或者记录session等。
注册页面:reg.html
form action="reg.php" method="POST"
table
trtd用户名:/tdtdinput type="username" size="20"/td/tr
trtd密码:/tdtdinput type="userpass" size="20"/td/tr
trtd确认密码:/tdtdinput type="ruserpass" size="20"/td/tr
trtd邮箱:/tdtdinput type="email" size="50"/td/tr
trtd电话:/tdtdinput type="telphone" size="20"/td/tr
trtdinput type="Submit" value="注册"/td/tr
/table
/form
接收页面:reg.php
%php
$db = mysql_connect("localhost", "root", "12345");
mysql_select_db("dataname", $db);
mysql_query("insert into tablename(username, userpass, email, telphone) values('$_POST[username]', '$_POST[userpass]', '$_POST[email]', '$_POST[telphone]')");
echo "注册成功";
%
结语:以上就是首席CTO笔记为大家整理的关于php怎么注册链接数据库的相关内容解答汇总了,希望对您有所帮助!如果解决了您的问题欢迎分享给更多关注此问题的朋友喔~