快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

简单php网站带数据库,简单php网站带数据库功能

PHP网站怎么连接到数据库?

常规方式

10年积累的成都做网站、网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站策划后付款的网站建设流程,更有安平免费网站建设让你可以放心的选择与我们合作。

常规方式就是按部就班的读取文件了。其余的话和上述方案一致。

// 读取配置文件内容

$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 );

}

}

}

如何做一个简单PHP网站

你试试看使用模板吧,所有的界面设计都是针对一个静态网页进行,这个静态网页里面所有你需要的内容都使用PHP的变量(或者你自己规定的其它特殊格式)来代表,设计布局的时候都是对这么静态网页进行操作。

而网站并不直接向外显示网页,网页的所有内容有PHP程序从数据库里面获取,对网页模板里面的变量进行替换后输出。

例如你的首页模板可以命名为index.htm,实际使用index.php来显示首页,PHP的流程是这样的:

?php

//链接数据库,获取各类数据到变量中

$news='例如新闻内容';

//获取模板

$html=file_get_content('index.htm');

//替换模板中的变量

$html=str_replace('--news--',$news,$html);

//输出模板

echo $html;

?

怎么用php写一个简单的登陆页面,要连上数据库

数据库是SQLserver

?

if(isset($_GET['username']))

{

session_start();

$errormsg = "";

$input['username'] = strtolower(trim($_GET['username']));

if($errormsg == "")

{

include("db_link.php");//你自己SQL数据库所在路径

$sql = "select

user_id,

username,

status

from guestbook

where username = '".$input['username']."' and status = 1";

$result = mysql_query($sql, $link) or die('Query database failed');

$num = mysql_num_rows($result);

if($num 1)

{

$errormsg = "用户名不正确,请重新登录!";

}

else

{

$row = mysql_fetch_array($result);

if($row['username'] == $input['username'])

{

$_SESSION['s_user_id'] = $row['user_id'];

$_SESSION['s_username'] = $row['username'];

$_SESSION['s_status'] = $row['status'];

$_SESSION['time_last_load'] = time();

?

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

html

head

title管理员登录/title

style

button,input{font-size:12px;padding-top:2px;font-family : Arial, Helvetica, sans-serif;}

a:link:{color:#666666;}

a:visited:{color:#666666;}

a:hover:{color:#000000}

/style

/head

body style="font-size: 12px;"

table width="280" border="0" cellspacing="0" cellpadding="0" align="center" style="font-size: 12px;border:1px solid #639ECE;"

trtd height="10"/td/tr

trtd height="40" align="center"登录成功!/td/tr

trtd height="10"/td/tr

trtd height="22" align="right" bgcolor="#EFFBFF"a href="javascript:void(null);" onclick="window.close();"关闭窗口/a /td/tr

/table

/body

/html

?

}

else

{

$errormsg = "用户名不正确,请重新登录!";

}

}

}

if($errormsg "")

{

?

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

html

head

title管理员登录/title

style

button,input{font-size:12px;padding-top:2px;}

/style

/head

body style="font-size: 12px;"

table width="280" border="0" cellspacing="0" cellpadding="5" align="center" style="font-size: 12px;border:1px solid #639ECE;"

trtd height="10" colspan="2"/td/tr

form action="userlogin.php"

tr

td width="60" align="right"用户名:/td

td width="220"input type="text" name="username" value="?=$input['username']?"/td

/tr

tr

td/td

td align="center" input type="submit" value=" 登录 "/td

/tr

/form

tr

td height="10" colspan="2"/td

/tr

tr

td height="22" colspan="2" bgcolor="#EFFBFF"?=$errormsg?/td

/tr

/table

/body

/html

?

}

}

else

{

?

!--登录页面--

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

html

head

title管理员登录/title

style

button,input{

font-size:12px;

padding-top:2px;

}

/style

/head

body

table width="280" border="0" cellspacing="0" cellpadding="5" align="center" style="font-size: 12px;border:1px solid #639ECE;"

trtd height="10" colspan="2"/td/tr

form action="userlogin.php"

tr

td width="60" align="right"用户名:/td

td width="220"input type="text" name="username"/td

/tr

tr

td/td

td align="center" input type="submit" value=" 登录 "/td

/tr

/form

trtd height="10" colspan="2"/td/tr

/table

/body

/html

?

}

?

如何实现最简单的php网页+mysql查询功能

首先搭建一个PHP环境,我用的wamp

然后比如你的数据库位置是本地localhost

数据库用户名是root

数据库密码是123456

数据库名是mydb

数据库里有个表mytab

有3个字段

id(主键) name sno

1 张三 123

2 李四 456

然后在项目根目录,新建一个文件:index.php

?php

//连接数据库

$con=mysqli_connect("localhost","root","123456","mydb");

//SQL语句

$sql="select * from mytab;";

//执行SQL语句,结果保存到$arr

$obj=mysqli_query($con,$sql);

$arr=mysqli_num_rows($result);

?

html

head

meta http-equiv="Content-Type" content="text/html; charset=UTF-8"

title实现最简单的php网页+mysql查询功能/title

/head

body

?php

echo "pre";

print_r($obj);

?

/body

/html

之后就能够看到结果了


分享文章:简单php网站带数据库,简单php网站带数据库功能
文章位置:http://6mz.cn/article/dsesegj.html

其他资讯