代碼如下:?View

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:國際域名空間、雅安服務(wù)器托管、營銷軟件、網(wǎng)站建設(shè)、張灣網(wǎng)站維護(hù)、網(wǎng)站推廣。
Code
PHP
include("conn.php");//調(diào)用數(shù)據(jù)庫連接文件
echo
"table
width=572
height=56
border=0
cellspacing=1
";
//創(chuàng)建html表格
echo
"tr
bgcolor=#9999FF";
echo
"th
width=33
scope=colid/th";
echo
"th
width=100
scope=coluser_name/th
";
echo
"th
width=100
scope=coluser_pass/th
";
echo
"th
width=100
scope=colstaus/th";
echo
"th
width=100
scope=colinsert_time/th";
echo
"/tr";
$SQL
=
"select
*
from
user_info";
$query
=
mysql_query($SQL);
//SQL查詢語句
while
($row
=
mysql_fetch_array($query)){
//使用while循環(huán)mysql_fetch_array()并將數(shù)據(jù)返回?cái)?shù)組
echo
"tr
onmouseout=this.style.backgroundColor=''
onMouseOver=this.style.backgroundColor='#99CC33'
bgcolor=#CCCCCC";
echo
"td$row[0]/td";
//輸出數(shù)組中數(shù)據(jù)
echo
"td$row[1]/td";
echo
"td$row[2]/td";
echo
"td$row[3]/td";
echo
"td$row[4]/td";
echo
"/tr";
}
echo
"/table";輸出記錄截圖
連接到一個(gè)?url?地址為localhost?、?端口為?3306?的mysql服務(wù)器上。mysql服務(wù)器的帳號是"root",密碼是"9999"。mysql?服務(wù)器上有一個(gè)數(shù)據(jù)庫?ok?,?數(shù)據(jù)庫里有一個(gè)表?abc。表?abc?一共為兩列,列名分別是?"id"?和?"name"?,將?abc?里的所有數(shù)據(jù)讀出來。
??
$dbh?=?@mysql_connect("localhost:3306","root","9999");?
/*?定義變量dbh?,?mysql_connect()函數(shù)的意思是連接mysql數(shù)據(jù)庫,?"@"的意思是屏蔽報(bào)錯(cuò)?*/?
if(!$dbh){die("error");}?
/*?die()函數(shù)的意思是將括號里的字串送到瀏覽器并中斷PHP程式?(Script)。括號里的參數(shù)為欲送出的字串。?*/?
@mysql_select_db("ok",?$dbh);?
/*?選擇mysql服務(wù)器里的一個(gè)數(shù)據(jù)庫,這里選的數(shù)據(jù)庫名為?ok?*/?
$q?=?"SELECT?*?FROM?abc";?
/*?定義變量q,?"SELECT?*?FROM?abc"是一個(gè)SQL語句,意思是讀取表abc中的數(shù)據(jù)?*/?
??
br?/?
!--=========?方法一?=========--?
br?/?
??
$rs?=?mysql_query($q,?$dbh);?
/*?定義變量?rs?,函數(shù)mysql_query()的意思是:送出?query?字串供?MySQL?做相關(guān)的處理或者執(zhí)行.由于php是從右往左執(zhí)行的,所以,rs的值是服務(wù)器運(yùn)行mysql_query()函數(shù)后返回的值?*/?
if(!$rs){die("Valid?result!");}?
echo?"table";?
echo?"trtdID/tdtdName/td/tr";?
while($row?=?mysql_fetch_row($rs))?echo?"trtd$row[0]/tdtd$row[1]/td/tr";?
/*?定義量變(數(shù)組)row,并利用while循環(huán),把數(shù)據(jù)一一寫出來.??
函數(shù)mysql_fetch_row()的意思是:將查詢結(jié)果$rs單列拆到陣列變數(shù)中.??
$row[0]?和?$row[1]?的位置可以換*/?
echo?"/table";?
??
br?/?
!--=========?方法二?=========--?
br?/?
??
$rs?=?mysql_query($q,?$dbh);?
while($row?=?mysql_fetch_object($rs))?echo?"$row-id?$row-name?br?/";?
/*?id和name可以換位置?*/?
??
br?/?
!--=========?方法三?=========--?
br?/?
??
$rs?=?mysql_query($q,?$dbh);?
while($row?=?mysql_fetch_array($rs))?echo?"$row[id]?$row[name]?br?/";?
/*?id和name可以換位置?*/?
??
!--=========?方法三最快?=========--?
??
@mysql_close($dbh);?
/*?關(guān)閉到mysql數(shù)據(jù)庫的連接?*/?
?
常規(guī)方式
常規(guī)方式就是按部就班的讀取文件了。其余的話和上述方案一致。
// 讀取配置文件內(nèi)容
$handle = fopen("filepath", "r"); ? ? ? ? ? ?$content = fread($handle, filesize("filepath"));123
PHP解析XML
上述兩種讀取文件,其實(shí)都是為了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)順序書寫.其實(shí)也無所謂了 --
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é)點(diǎn),進(jìn)而獲取相關(guān)的數(shù)據(jù)庫信息
$mysql = simplexml_load_string($content); ? ? ? ? ? ?// 將獲取到的xml節(jié)點(diǎn)信息賦值給關(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ù)庫配置文件信息出錯(cuò)!/markbr /" );
} ? ? ? ?return $dbconfig;
}
}1234567891011121314151617181920212223242526272829
數(shù)據(jù)庫連接池
對于PHP程序而言,優(yōu)化永無止境。而數(shù)據(jù)庫連接池就在一定程度上起到了優(yōu)化的作用。其使得對用戶的每一個(gè)請求而言,無需每次都像數(shù)據(jù)庫申請鏈接資源。而是通過已存在的數(shù)據(jù)庫連接池中的鏈接來返回,從時(shí)間上,效率上,都是一個(gè)大大的提升。
于是,這里簡單的模擬了一下數(shù)據(jù)庫連接池的實(shí)現(xiàn)。核心在于維護(hù)一個(gè)“池”。
從池子中取,用畢,歸還給池子。
?php/**x
* ?PHP中的數(shù)據(jù)庫 工具類設(shè)計(jì)
* ?郭璞
* ?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ù)庫連接池“偽隊(duì)列”
$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ù)庫連接池中獲取一個(gè)數(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 );
}
}
}
$con=mysql_connect('localhost','root','');//數(shù)據(jù)庫信息
mysql_select_db('shop');//數(shù)據(jù)庫名
mysql_query("set?names?utf8");//設(shè)置字符集編碼
$sql="select?goods_name,goods_number,shop_price?from?goods";//查詢語句
$res=mysql_query($sql);//執(zhí)行查詢
while($row=mysql_fetch_assoc($res)){
$rows[]=$row;//接受結(jié)果集
}
//遍歷數(shù)組
foreach($rows?as?$key=$v){
echo?$v['goods_name']."---".$v['goods_number']."---".$v['shop_price']."";
}
布局可以自己寫的。數(shù)據(jù)從foreach循環(huán)里取出。
?php
$link=mysql_connect('localhost','用戶名','密碼')or?die("數(shù)據(jù)庫連接失敗");//連接數(shù)據(jù)庫
mysql_select_db('數(shù)據(jù)庫名',$link);//選擇數(shù)據(jù)庫
mysql_query("set?names?utf8");//設(shè)置編碼格式
$q="select?*?from?"數(shù)據(jù)表";//設(shè)置查詢指令
$result=mysql_query($q);//執(zhí)行查詢
while($row=mysql_fetch_assoc($result))//將result結(jié)果集中查詢結(jié)果取出一條
{?echo??返回到HTML;?}
?
html界面使用ajax的成功返回值,再渲染在界面里就行了
用PHP自帶函數(shù)就可以實(shí)現(xiàn),首先要過去對方的網(wǎng)頁信息,用
file_get_contents();參數(shù)是對方的URL地址,這個(gè)函數(shù)返回是一個(gè)字符串你想要的東西就在這個(gè)字符串中了
接下來就可以針對這個(gè)字符串做處理了,說下思路,正如你這個(gè)問題想獲取到航班號起飛時(shí)間,在這個(gè)網(wǎng)頁中應(yīng)該有很多相同的標(biāo)簽元素,它們都有共同點(diǎn),用
用正則表達(dá)式preg_match();或者是
preg_match_all();這兩個(gè)函數(shù)它們都返回一個(gè)數(shù)組,這個(gè)數(shù)組存的就是你要的航班號和起飛時(shí)間,那么相同信息的數(shù)組就會(huì)出現(xiàn)了,然后在對這個(gè)數(shù)組進(jìn)行分析找到你要的某個(gè)值或全部的值
獲取信息要用到的3個(gè)函數(shù)是:
file_get_contents();
preg_match();
preg_match_all();
當(dāng)前名稱:php獲取別網(wǎng)站的數(shù)據(jù)庫,php獲取別網(wǎng)站的數(shù)據(jù)庫信息
文章位置:http://chinadenli.net/article4/heodie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、關(guān)鍵詞優(yōu)化、虛擬主機(jī)、品牌網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)公司、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)