常規(guī)方式

10年積累的成都做網(wǎng)站、網(wǎng)站設(shè)計經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站策劃后付款的網(wǎng)站建設(shè)流程,更有安平免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
常規(guī)方式就是按部就班的讀取文件了。其余的話和上述方案一致。
// 讀取配置文件內(nèi)容
$handle = fopen("filepath", "r"); ? ? ? ? ? ?$content = fread($handle, filesize("filepath"));123
PHP解析XML
上述兩種讀取文件,其實都是為了PHP解析XML來做準(zhǔn)備的。關(guān)于PHP解析XML的方式的博客有很多。方式也有很多,像simplexml,XMLReader,DOM啦等等。但是對于比較小型的xml配置文件,simplexml就足夠了。
配置文件
?xml version="1.0" encoding="UTF-8" ?mysql
!-- 為防止出現(xiàn)意外,請按照此標(biāo)準(zhǔn)順序書寫.其實也無所謂了 --
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 { ? ? ? ? ? ?// 讀取配置文件內(nèi)容
$handle = fopen(self::$dbconfigpath, "r"); ? ? ? ? ? ?$content = fread($handle, filesize(self::$dbconfigpath)); ? ? ? ? ? ?// 獲取xml文檔根節(jié)點,進(jìn)而獲取相關(guān)的數(shù)據(jù)庫信息
$mysql = simplexml_load_string($content); ? ? ? ? ? ?// 將獲取到的xml節(jié)點信息賦值給關(guān)聯(lián)數(shù)組,方便接下來的方法調(diào)用
$dbconfig['host'] = $mysql-host; ? ? ? ? ? ?$dbconfig['user'] = $mysql-user; ? ? ? ? ? ?$dbconfig['password'] = $mysql-password; ? ? ? ? ? ?$dbconfig['db'] = $mysql-db; ? ? ? ? ? ?$dbconfig['port'] = $mysql-port; ? ? ? ? ? ?// 將配置信息以關(guān)聯(lián)數(shù)組的形式返回
return $dbconfig;
} catch ( Exception $e ) { ? ? ? ? ? ?throw new RuntimeException ( "mark讀取數(shù)據(jù)庫配置文件信息出錯!/markbr /" );
} ? ? ? ?return $dbconfig;
}
}1234567891011121314151617181920212223242526272829
數(shù)據(jù)庫連接池
對于PHP程序而言,優(yōu)化永無止境。而數(shù)據(jù)庫連接池就在一定程度上起到了優(yōu)化的作用。其使得對用戶的每一個請求而言,無需每次都像數(shù)據(jù)庫申請鏈接資源。而是通過已存在的數(shù)據(jù)庫連接池中的鏈接來返回,從時間上,效率上,都是一個大大的提升。
于是,這里簡單的模擬了一下數(shù)據(jù)庫連接池的實現(xiàn)。核心在于維護(hù)一個“池”。
從池子中取,用畢,歸還給池子。
?php/**x
* ?PHP中的數(shù)據(jù)庫 工具類設(shè)計
* ?郭璞
* ?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文件丟失,無法進(jìn)行配置文件的初始化操作!/markbr /" );
}else {
require './utils.php';
} ? ? ? ?// 初始化 配置文件信息
$this-dbconfig = XMLUtil::getDBConfiguration (); ? ? ? ?// 準(zhǔn)備好數(shù)據(jù)庫連接池“偽隊列”
$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連接數(shù)據(jù)庫失敗!/markbr /" );
array_push ( $this-dbpool, $conn );
}
} ? ?/**
* 從數(shù)據(jù)庫連接池中獲取一個數(shù)據(jù)庫鏈接資源
*
* @throws ErrorException
* @return mixed
*/
public function getConn() { ? ? ? ?if (count ( $this-dbpool ) = 0) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫連接池中已無鏈接資源,請稍后重試!/mark" );
} else { ? ? ? ? ? ?return array_pop ( $this-dbpool );
}
} ? ?/**
* 將用完的數(shù)據(jù)庫鏈接資源放回到數(shù)據(jù)庫連接池
*
* @param unknown $conn
* @throws ErrorException
*/
public function release($conn) { ? ? ? ?if (count ( $this-dbpool ) = $this-poolsize) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫連接池已滿/markbr /" );
} else {
array_push ( $this-dbpool, $conn );
}
}
}
你試試看使用模板吧,所有的界面設(shè)計都是針對一個靜態(tài)網(wǎng)頁進(jìn)行,這個靜態(tài)網(wǎng)頁里面所有你需要的內(nèi)容都使用PHP的變量(或者你自己規(guī)定的其它特殊格式)來代表,設(shè)計布局的時候都是對這么靜態(tài)網(wǎng)頁進(jìn)行操作。
而網(wǎng)站并不直接向外顯示網(wǎng)頁,網(wǎng)頁的所有內(nèi)容有PHP程序從數(shù)據(jù)庫里面獲取,對網(wǎng)頁模板里面的變量進(jìn)行替換后輸出。
例如你的首頁模板可以命名為index.htm,實際使用index.php來顯示首頁,PHP的流程是這樣的:
?php
//鏈接數(shù)據(jù)庫,獲取各類數(shù)據(jù)到變量中
$news='例如新聞內(nèi)容';
//獲取模板
$html=file_get_content('index.htm');
//替換模板中的變量
$html=str_replace('--news--',$news,$html);
//輸出模板
echo $html;
?
數(shù)據(jù)庫是SQLserver
?
if(isset($_GET['username']))
{
session_start();
$errormsg = "";
$input['username'] = strtolower(trim($_GET['username']));
if($errormsg == "")
{
include("db_link.php");//你自己SQL數(shù)據(jù)庫所在路徑
$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();"關(guān)閉窗口/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環(huán)境,我用的wamp
然后比如你的數(shù)據(jù)庫位置是本地localhost
數(shù)據(jù)庫用戶名是root
數(shù)據(jù)庫密碼是123456
數(shù)據(jù)庫名是mydb
數(shù)據(jù)庫里有個表mytab
有3個字段
id(主鍵) name sno
1 張三 123
2 李四 456
然后在項目根目錄,新建一個文件:index.php
?php
//連接數(shù)據(jù)庫
$con=mysqli_connect("localhost","root","123456","mydb");
//SQL語句
$sql="select * from mytab;";
//執(zhí)行SQL語句,結(jié)果保存到$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實現(xiàn)最簡單的php網(wǎng)頁+mysql查詢功能/title
/head
body
?php
echo "pre";
print_r($obj);
?
/body
/html
之后就能夠看到結(jié)果了
本文名稱:簡單php網(wǎng)站帶數(shù)據(jù)庫,簡單php網(wǎng)站帶數(shù)據(jù)庫功能
當(dāng)前地址:http://chinadenli.net/article17/dsesegj.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、響應(yīng)式網(wǎng)站、網(wǎng)站建設(shè)、全網(wǎng)營銷推廣、面包屑導(dǎo)航、手機網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)