create databases handle;
為瑞安等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及瑞安網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為做網(wǎng)站、網(wǎng)站制作、瑞安網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
create table user(
id int unsigned not null auto_increment primary key,
name varchar(8),
sex tinyint(1) default '1',
score int not null,
state tinyint(1)
);
2.向表中添加數(shù)據(jù)(不示例)
3.創(chuàng)建index.html客戶端,a.php服務(wù)端1,b.php服務(wù)端2
Index.html:
!DOCTYPE html
html lang="en"
head
meta charset="UTF-8"
title客戶端/title
/head
body
button onclick="send('a.php?state=0')"開始請(qǐng)求/button
div style="position: fixed;width: 500px;height: 300px;top: 100px;background: gray"
span style="color: white;font-size: 20px;"/span
/div
script type="text/javascript" src="./jquery-1.10.2.min.js"/script
script type="text/javascript"
//創(chuàng)建一個(gè)模態(tài)框
function display(value){
$('span').html(value);
}
//ajax
function send(dizhi){
$.ajax({
type: "get",
url: dizhi,
success: function(msg){
var arr=JSON.parse(msg);
console.log(arr);
//alert(arr.value);
var tishi="已經(jīng)處理 "+arr.now +"個(gè),共"+arr.all+"個(gè)";
display(tishi);
if(arr.now!=arr.all){
send("a.php?now="+arr.now+"all="+arr.all);
}else{
alert("完成!");
}
}
});
}
/script
/body
/html
a.php:
?php
require('./dbconfig.php');
$link=mysql_connect(HOST,USER,PASS) or die('數(shù)據(jù)庫(kù)鏈接失敗');
mysql_select_db(DBNAME);
/*
查詢數(shù)據(jù)
$sql="select * from user";
$result=mysql_query($sql);
$row=mysql_fetch_assoc($result);
var_dump($row);
*/
/*
循環(huán)插入
for($i=3;$i=100;$i++){
$sql= "insert into user(name,score,state) values('z".$i."',".$i.",1)";
mysql_query($sql);
}
*/
/*查詢需要處理的數(shù)據(jù)總數(shù)*/
//isset($_GET['state'])?$_GET['state']:0;
if(isset($_GET['state'])){
$sql="select count(*) from user";
$result=mysql_query($sql);
$all=mysql_result($result,0);
$now=0;
header("Location: b.php?all={$all}now=0");
}else{
header("Location: b.php?all={$_GET['all']}now={$_GET['now']}");
}
/*返回當(dāng)前處理的數(shù)據(jù)*/
b.php:
?php
require('./dbconfig.php');
$link=mysql_connect(HOST,USER,PASS) or die('數(shù)據(jù)庫(kù)鏈接失敗');
mysql_select_db(DBNAME);
/*返回當(dāng)前處理的數(shù)據(jù)*/
//$id=$_GET['id'];//獲取將要處理的id
$now=$_GET['now'];//已經(jīng)處理的個(gè)數(shù)
$all=$_GET['all'];//總共要處理的個(gè)數(shù)
$sql="select score from user limit {$now},1";
$result=mysql_query($sql);
$value=mysql_result($result, 0);
$now++;
$arr=array(
'now'=$now,
'all'=$all,
'value'=$value
);
//print_r($arr);
echo json_encode($arr);
dbconfig.php:
?php
define('HOST','127.0.0.1');
define('USER', 'root');
define('PASS','root');
define('DBNAME','handle');
從查詢結(jié)果取值,需要遍歷結(jié)果集!示例如下:
$rs?=?mysql_query("select?*?from?www_liu?where?xx='$xx'?and?yy='$yy'");
echo?"查詢信息如下:br/";
while($row?=?mysql_fetch_array($rs))
{
echo?$row['字段2']?.?"====="?.?$row['字段三'];
echo?"br?/";
}
//關(guān)閉數(shù)據(jù)庫(kù)連接
//mysql_close();
在insert之后就要判斷是否加成功 是的話返回保存成功,否就返回不成功。
在前端的回調(diào)函數(shù)里json_decode()你傳過(guò)來(lái)的數(shù)據(jù) 就可以用了
用PHP自帶函數(shù)就可以實(shí)現(xiàn),首先要過(guò)去對(duì)方的網(wǎng)頁(yè)信息,用
file_get_contents();參數(shù)是對(duì)方的URL地址,這個(gè)函數(shù)返回是一個(gè)字符串你想要的東西就在這個(gè)字符串中了
接下來(lái)就可以針對(duì)這個(gè)字符串做處理了,說(shuō)下思路,正如你這個(gè)問題想獲取到航班號(hào)起飛時(shí)間,在這個(gè)網(wǎng)頁(yè)中應(yīng)該有很多相同的標(biāo)簽元素,它們都有共同點(diǎn),用
用正則表達(dá)式preg_match();或者是
preg_match_all();這兩個(gè)函數(shù)它們都返回一個(gè)數(shù)組,這個(gè)數(shù)組存的就是你要的航班號(hào)和起飛時(shí)間,那么相同信息的數(shù)組就會(huì)出現(xiàn)了,然后在對(duì)這個(gè)數(shù)組進(jìn)行分析找到你要的某個(gè)值或全部的值
獲取信息要用到的3個(gè)函數(shù)是:
file_get_contents();
preg_match();
preg_match_all();
常規(guī)方式
常規(guī)方式就是按部就班的讀取文件了。其余的話和上述方案一致。
// 讀取配置文件內(nèi)容
$handle = fopen("filepath", "r"); ? ? ? ? ? ?$content = fread($handle, filesize("filepath"));123
PHP解析XML
上述兩種讀取文件,其實(shí)都是為了PHP解析XML來(lái)做準(zhǔn)備的。關(guān)于PHP解析XML的方式的博客有很多。方式也有很多,像simplexml,XMLReader,DOM啦等等。但是對(duì)于比較小型的xml配置文件,simplexml就足夠了。
配置文件
?xml version="1.0" encoding="UTF-8" ?mysql
!-- 為防止出現(xiàn)意外,請(qǐng)按照此標(biāo)準(zhǔn)順序書寫.其實(shí)也無(wú)所謂了 --
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ù)庫(kù)信息
$mysql = simplexml_load_string($content); ? ? ? ? ? ?// 將獲取到的xml節(jié)點(diǎn)信息賦值給關(guān)聯(lián)數(shù)組,方便接下來(lái)的方法調(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ù)庫(kù)配置文件信息出錯(cuò)!/markbr /" );
} ? ? ? ?return $dbconfig;
}
}1234567891011121314151617181920212223242526272829
數(shù)據(jù)庫(kù)連接池
對(duì)于PHP程序而言,優(yōu)化永無(wú)止境。而數(shù)據(jù)庫(kù)連接池就在一定程度上起到了優(yōu)化的作用。其使得對(duì)用戶的每一個(gè)請(qǐng)求而言,無(wú)需每次都像數(shù)據(jù)庫(kù)申請(qǐng)鏈接資源。而是通過(guò)已存在的數(shù)據(jù)庫(kù)連接池中的鏈接來(lái)返回,從時(shí)間上,效率上,都是一個(gè)大大的提升。
于是,這里簡(jiǎn)單的模擬了一下數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn)。核心在于維護(hù)一個(gè)“池”。
從池子中取,用畢,歸還給池子。
?php/**x
* ?PHP中的數(shù)據(jù)庫(kù) 工具類設(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文件丟失,無(wú)法進(jìn)行配置文件的初始化操作!/markbr /" );
}else {
require './utils.php';
} ? ? ? ?// 初始化 配置文件信息
$this-dbconfig = XMLUtil::getDBConfiguration (); ? ? ? ?// 準(zhǔn)備好數(shù)據(jù)庫(kù)連接池“偽隊(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ù)庫(kù)失敗!/markbr /" );
array_push ( $this-dbpool, $conn );
}
} ? ?/**
* 從數(shù)據(jù)庫(kù)連接池中獲取一個(gè)數(shù)據(jù)庫(kù)鏈接資源
*
* @throws ErrorException
* @return mixed
*/
public function getConn() { ? ? ? ?if (count ( $this-dbpool ) = 0) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫(kù)連接池中已無(wú)鏈接資源,請(qǐng)稍后重試!/mark" );
} else { ? ? ? ? ? ?return array_pop ( $this-dbpool );
}
} ? ?/**
* 將用完的數(shù)據(jù)庫(kù)鏈接資源放回到數(shù)據(jù)庫(kù)連接池
*
* @param unknown $conn
* @throws ErrorException
*/
public function release($conn) { ? ? ? ?if (count ( $this-dbpool ) = $this-poolsize) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫(kù)連接池已滿/markbr /" );
} else {
array_push ( $this-dbpool, $conn );
}
}
}
文章題目:php處理網(wǎng)址返回?cái)?shù)據(jù)庫(kù) php查詢數(shù)據(jù)庫(kù)數(shù)據(jù)返回?cái)?shù)據(jù)
當(dāng)前URL:http://muchs.cn/article16/docdggg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、App開發(fā)、網(wǎng)站建設(shè)、小程序開發(fā)、搜索引擎優(yōu)化、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)