❶ php使用mysqli和pdo擴展,測試對比mysql資料庫的執行效率完整示例
本文實例講述了php使用mysqli和pdo擴展,測試對比mysql資料庫的執行效率。分享給大家供大家參考,具體如下:
<?php
/**
*
測試pdo和mysqli的執行效率
*/
header("Content-type:text/html;charset=utf-8");
//通過pdo鏈接資料庫
$pdo_startTime
=
microtime(true);
$pdo
=
new
PDO("mysql:host=localhost;dbname=test","root","1234",array(PDO::MYSQL_ATTR_INIT_COMMAND
=>
"SET
NAMES'utf8';"));
for($i=1;$i<=100;$i++){
$title
=
"pdo標題".$i;
$content
=
"pdo內容".$i;
$addtime
=
time();
$user_id
=
$i;
$pdo_sql
=
"INSERT
INTO
`article`(`title`,`content`,`addtime`,`user_id`)
VALUES(:title,:content,:addtime,:user_id)";
$sth
=
$pdo->prepare($pdo_sql);
$sth->bindParam(':title',$title);
$sth->bindParam(':content',$content);
$sth->bindParam(':addtime',$addtime);
$sth->bindParam(':user_id',$user_id);
$sth->execute();
}
$pdo_endTime
=
microtime(true);
$pdo_time
=
$pdo_endTime
-
$pdo_startTime;
echo
$pdo_time;
echo
"<hr/>";
//通過mysql鏈接資料庫
$mysqli_startTime
=
microtime(true);
$mysqli
=
mysqli_connect("localhost","root","1234","test")
or
die("數據連接失敗");
mysqli_query($mysqli,"set
names
utf8");
for($i=1;$i<=100;$i++){
$title
=
"mysqli標題".$i;
$content
=
"mysqli內容".$i;
$addtime
=
time();
$user_id
=
$i;
$sql
=
"INSERT
INTO
`article`(`title`,`content`,`addtime`,`user_id`)
VALUES('".$title."','".$content."',".$addtime.",".$user_id.")";
mysqli_query($mysqli,$sql);
}
$mysqli_endTime
=
microtime(true);
$mysqli_time
=
$mysqli_endTime
-
$mysqli_startTime;
echo
$mysqli_time;
echo
"<hr/>";
if($pdo_time
>
$mysqli_time){
echo
"pdo的執行時間是mysqli的".round($pdo_time/$mysqli_time)."倍";
}else{
echo
"mysqli的執行時間是pdo的".round($mysqli_time/$pdo_time)."倍";
}
測試結果:其實經過多次測試,pdo和mysqli的執行效率差不多。
更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP基於pdo操作資料庫技巧總結》、《php+mysqli資料庫程序設計技巧總結》、《php面向對象程序設計入門教程》、《php字元串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:php使用mysqli和pdo擴展,測試對比連接mysql資料庫的效率完整示例php中資料庫連接方式pdo和mysqli對比分析php中關於mysqli和mysql區別的一些知識點分析php操作mysqli(示例代碼)php封裝的mysqli類完整實例PHP以mysqli方式連接類完整代碼實例php簡單解析mysqli查詢結果的方法(2種方法)php中mysql連接方式PDO使用詳解Php中用PDO查詢Mysql來避免SQL注入風險的方法php
mysql
PDO
查詢操作的實例詳解PHP實現PDO的mysql資料庫操作類
❷ PHP 求一個支持oracle的pdo封裝類
建議你參考下zend framework的支持oracle的pdo封裝類
❸ 我PHP用PDO可以操作MYSQL資料庫 那我為什麼還要去弄框架可以操作資料庫我還有封裝的必要嗎舉個例子
PDO配置。打開php.ini配置文件,找到下圖所示的配置信息,去掉要啟用的PDO前面的「#」號即可。另外一種方式是直接在啟動的wampserver中找到php擴展中的php_pdo_db.lib選項,重啟wampserver伺服器即可。如何利用PDO連接資料庫。利用下面這條簡單的語句即可連接資料庫$pdo=newPDO("mysql:host=localhost;dbname=php100","root",「");其中具體參數介紹如下圖所示:PDO中常用的函數及其解釋如下。PDO::query()主要是用於有記錄結果返回的操作,特別是SELECT操作PDO::exec()主要是針對沒有結果集合返回的操作,如INSERT、UPDATE等操作PDO::lastInsertId()返回上次插入操作,主鍵列類型是自增的最後的自增IDPDOStatement::fetch()是用來獲取一條記錄PDOStatement::fetchAll()是獲取所有記錄集到一個中下面通過一個簡單的php代碼示例來具體介紹如何使用PDO進行資料庫操作。exec("insertintouser_list(uid,m_id,username,password)values(null,'3','testpdo','testpdo')");//使用查詢語句$sr=$pdo->query("select*fromuser_list");//將查詢的結果循環輸出顯示while($row=$sr->fetch()){print_r($row);}?>
❹ php中的pdo是什麼
PDO擴展為PHP訪問資料庫定義了一個輕量級的、一致性的介面,它提供了一個數據訪問抽象層,這樣,無論使用什麼資料庫,都可以通過一致的函數執行查詢和獲取數據。PDO隨PHP5.1發行,在PHP5.0的PECL擴展中也可以使用。
❺ php連接資料庫關於PDO類和PDOStatement類的區別是什麼
這代表兩個不同的對象,看一下PDO::query()的定義:
public PDOStatement PDO::query ( string $statement )
可以知道query返回的結果是PDOStatement實例對象,那麼你至少可以知道PDOStatement可以用來操作結果集了。
看一下PDOStatement類有哪些方法:
http://php.net/manual/zh/class.pdostatement.php
發現有fetch,fetchAll等熟悉的功能,不是么?
然後再來看看PDO::prepare()方法
public PDOStatement PDO::prepare ( string $statement [, array $driver_options = array() ] )
它也是返回的PDOStatement,按照手冊的說法,PDOStatement代表一條預處理語句,並在該語句被執行後代表一個相關的結果集。
也就是說,如果你通過PDO::query()直接查詢,那麼返回的PDOStatement就表示一個結果集。
如果你通過PDO::prepare()查詢,實際上並未執行sql,而是得到一個預處理語句(PDOStatement),然後你調用PDOStatement::execute()方法真正執行。
❻ php跪求封裝好的pdo預處理類,在線等
<?php
//資料庫連接類,不建議直接使用DB,而是對DB封裝一層
//這個類不會被污染,不會被直接調用
classDB{
//pdo對象
private$_pdo=null;
//用於存放實例化的對象
staticprivate$_instance=null;
//公共靜態方法獲取實例化的對象
(){
if(!(self::$_instanceinstanceofself)){
self::$_instance=newself();
}
returnself::$_instance;
}
//私有克隆
privatefunction__clone(){}
//私有構造
privatefunction__construct(){
try{
$this->_pdo=newPDO(DB_DNS,DB_USER,DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SETNAMES'.DB_CHARSET));
$this->_pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException$e){
exit($e->getMessage());
}
}
//新增
protectedfunctionadd($_tables,Array$_addData){
$_addFields=array();
$_addValues=array();
foreach($_addDataas$_key=>$_value){
$_addFields[]=$_key;
$_addValues[]=$_value;
}
$_addFields=implode(',',$_addFields);
$_addValues=implode("','",$_addValues);
$_sql="INSERTINTO$_tables[0]($_addFields)VALUES('$_addValues')";
return$this->execute($_sql)->rowCount();
}
//修改
protectedfunctionupdate($_tables,Array$_param,Array$_updateData){
$_where=$_setData='';
foreach($_paramas$_key=>$_value){
$_where.=$_value.'AND';
}
$_where='WHERE'.substr($_where,0,-4);
foreach($_updateDataas$_key=>$_value){
if(Validate::isArray($_value)){
$_setData.="$_key=$_value[0],";
}else{
$_setData.="$_key='$_value',";
}
}
$_setData=substr($_setData,0,-1);
$_sql="UPDATE$_tables[0]SET$_setData$_where";
return$this->execute($_sql)->rowCount();
}
//驗證一條數據
protectedfunctionisOne($_tables,Array$_param){
$_where='';
foreach($_paramas$_key=>$_value){
$_where.=$_value.'AND';
}
$_where='WHERE'.substr($_where,0,-4);
$_sql="SELECTidFROM$_tables[0]$_whereLIMIT1";
return$this->execute($_sql)->rowCount();
}
//刪除
protectedfunctiondelete($_tables,Array$_param){
$_where='';
foreach($_paramas$_key=>$_value){
$_where.=$_value.'AND';
}
$_where='WHERE'.substr($_where,0,-4);
$_sql="DELETEFROM$_tables[0]$_whereLIMIT1";
return$this->execute($_sql)->rowCount();
}
//查詢
protectedfunctionselect($_tables,Array$_fileld,Array$_param=array()){
$_limit=$_order=$_where=$_like='';
if(Validate::isArray($_param)&&!Validate::isNullArray($_param)){
$_limit=isset($_param['limit'])?'LIMIT'.$_param['limit']:'';
$_order=isset($_param['order'])?'ORDERBY'.$_param['order']:'';
if(isset($_param['where'])){
foreach($_param['where']as$_key=>$_value){
$_where.=$_value.'AND';
}
$_where='WHERE'.substr($_where,0,-4);
}
if(isset($_param['like'])){
foreach($_param['like']as$_key=>$_value){
$_like="WHERE$_keyLIKE'%$_value%'";
}
}
}
$_selectFields=implode(',',$_fileld);
$_table=isset($_tables[1])?$_tables[0].','.$_tables[1]:$_tables[0];
$_sql="SELECT$_selectFieldsFROM$_table$_where$_like$_order$_limit";
$_stmt=$this->execute($_sql);
$_result=array();
while(!!$_objs=$_stmt->fetchObject()){
$_result[]=$_objs;
}
returnTool::setHtmlString($_result);
}
//總記錄
protectedfunctiontotal($_tables,Array$_param=array()){
$_where='';
if(isset($_param['where'])){
foreach($_param['where']as$_key=>$_value){
$_where.=$_value.'AND';
}
$_where='WHERE'.substr($_where,0,-4);
}
$_sql="SELECTCOUNT(*)ascountFROM$_tables[0]$_where";
$_stmt=$this->execute($_sql);
return$_stmt->fetchObject()->count;
}
//得到下一個ID
protectedfunctionnextId($_tables){
$_sql="SHOWTABLESTATUSLIKE'$_tables[0]'";
$_stmt=$this->execute($_sql);
return$_stmt->fetchObject()->Auto_increment;
}
//執行SQL
privatefunctionexecute($_sql){
try{
$_stmt=$this->_pdo->prepare($_sql);
$_stmt->execute();
}catch(PDOException$e){
exit('SQL語句:'.$_sql.'<br/>錯誤信息:'.$e->getMessage());
}
return$_stmt;
}
}
?>