新聞中心
PHP網(wǎng)站怎么連接到數(shù)據(jù)庫(kù)?
常規(guī)方式
為尼木等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及尼木網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、尼木網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
常規(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ū)寫(xiě).其實(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 );
}
}
}
怎么測(cè)試android與php是否連接數(shù)據(jù)庫(kù)
參考下面代碼及代碼中的注釋即可:
PHP代碼:
conn.php是連接MySQL數(shù)據(jù)庫(kù)的。代碼如下:
?php
$dbhost = "localhost:3306";
$dbuser = "root"; //我的用戶名
$dbpass = ""; //我的密碼
$dbname = "testlogin"; //我的mysql庫(kù)名
$cn = mysql_connect($dbhost,$dbuser,$dbpass) or die("connect error");
@mysql_select_db($dbname)or die("db error");
mysql_query("set names 'UTF-8'");
?
login.php代碼:
?php
include ("conn.php");//連接數(shù)據(jù)庫(kù)
$username=str_replace(" ","",$_POST['name']);//接收客戶端發(fā)來(lái)的username;
$sql="select * from users where name='$username'";
$query=mysql_query($sql);
$rs = mysql_fetch_array($query);
if(is_array($rs)){
if($_POST['pwd']==$rs['password']){
echo "login succeed";
}else{
echo "error";
}
}
?
class LoginHandler implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
//get username and password;
userName = user_name.getText().toString().trim();
password = pass_word.getText().toString().trim();
//連接到服務(wù)器的地址,我監(jiān)聽(tīng)的是8080端口
String connectURL="網(wǎng)站地址/text0/com.light.text/login.php/";
//填入用戶名密碼和連接地址
boolean isLoginSucceed = gotoLogin(userName, password,connectURL);
//判斷返回值是否為true,若是的話就跳到主頁(yè)。
if(isLoginSucceed){
Intent intent = new Intent();
intent.setClass(getApplicationContext(), HomeActivity.class);
startActivity(intent);
proDialog.dismiss();
}else{
proDialog.dismiss();
// Toast.makeText(ClientActivity.this, "登入錯(cuò)誤", Toast.LENGTH_LONG).show();
System.out.println("登入錯(cuò)誤");
}
}
}
//登入的方法,傳入用戶 密碼 和連接地址
private boolean gotoLogin(String userName, String password,String connectUrl) {
String result = null; //用來(lái)取得返回的String;
boolean isLoginSucceed = false;
//test
System.out.println("username:"+userName);
System.out.println("password:"+password);
//發(fā)送post請(qǐng)求
HttpPost httpRequest = new HttpPost(connectUrl);
//Post運(yùn)作傳送變數(shù)必須用NameValuePair[]陣列儲(chǔ)存
List params = new ArrayList();
params.add(new BasicNameValuePair("name",userName));
params.add(new BasicNameValuePair("pwd",password));
try{
//發(fā)出HTTP請(qǐng)求
httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//取得HTTP response
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
//若狀態(tài)碼為200則請(qǐng)求成功,取到返回?cái)?shù)據(jù)
if(httpResponse.getStatusLine().getStatusCode()==200){
//取出字符串
result=EntityUtils.toString(httpResponse.getEntity());
ystem.out.println("result= "+result);
}
}catch(Exception e){
e.printStackTrace();
}
//判斷返回的數(shù)據(jù)是否為php中成功登入是輸出的
if(result.equals("login succeed")){
isLoginSucceed = true;
}
return isLoginSucceed;
}
php網(wǎng)站本地測(cè)試數(shù)據(jù)庫(kù)怎么連接?
?php
$conn = mysql_connect('localhost','用戶名','密碼') or die('連接數(shù)據(jù)庫(kù)失敗,請(qǐng)檢查您的數(shù)據(jù)庫(kù)配置');//登錄地址localhost,這個(gè)不行,試試127.0.0.1,有的時(shí)候沒(méi)有加映射localhost是連接不上的
mysql_select_db('數(shù)據(jù)庫(kù)名');//選中數(shù)據(jù)庫(kù)
?
當(dāng)前標(biāo)題:php+測(cè)試鏈接數(shù)據(jù)庫(kù) php 連接數(shù)據(jù)庫(kù)
當(dāng)前路徑:http://www.ef60e0e.cn/article/hjodei.html