A database operation class based on PDO

  • 2020-03-31 21:39:54
  • OfStack

Baidu decided to use PDO after, as to why choose PDO, here is no longer said, you can understand under baidu.
Since want to change, that is the most basic need to have a commonly used database operation class, that is, the so-called add delete change check and so on, last night for a night, roughly made a prototype, the following is the code, I hope you can give some advice.
 
<?php 
/* 
 Author: hu rui  
 Date: 2011/03/19 
 Email: hooray0905@foxmail.com 
20110319 
 Common database operations, such as: add, delete, change, get a single record, multiple records, return the latest one inserted record id , returns the number of rows of operation records, etc  
*/ 
 
function hrSelect($debug, $getcount, $getrow, $table, $fields="*", $sqlwhere="", $orderby="id desc"){ 
global $pdo; 
if($debug){ 
if($getcount){ 
echo "select count(*) from $table where 1=1 $sqlwhere order by $orderby"; 
}else{ 
echo "select $fields from $table where 1=1 $sqlwhere order by $orderby"; 
} 
exit; 
}else{ 
if($getcount){ 
$rs = $pdo->query("select count(*) from $table where 1=1 $sqlwhere order by $orderby"); 
return $rs->fetchColumn(); 
}elseif($getrow){ 
$rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere order by $orderby"); 
return $rs->fetch(); 
}else{ 
$rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere order by $orderby"); 
return $rs->fetchAll(); 
} 
} 
} 
 
function hrInsert($debug, $execrow, $lastinsertid, $table, $fields, $values){ 
global $pdo; 
if($debug){ 
echo "insert into $table ($fields) values ($values)"; 
exit; 
}elseif($execrow){ 
return $pdo->exec("insert into $table ($fields) values ($values)"); 
}elseif($lastinsertid){ 
return $pdo->lastInsertId("insert into $table ($fields) values ($values)"); 
}else{ 
$pdo->query("insert into $table ($fields) values ($values)"); 
} 
} 
 
function hrUpdate($debug, $execrow, $table, $set, $sqlwhere=""){ 
global $pdo; 
if($debug){ 
echo "update $table set $set where 1=1 $sqlwhere"; 
exit; 
}elseif($execrow){ 
return $pdo->exec("update $table set $set where 1=1 $sqlwhere"); 
}else{ 
$pdo->query("update $table set $set where 1=1 $sqlwhere"); 
} 
} 
 
function hrDelete($debug, $execrow, $table, $sqlwhere=""){ 
global $pdo; 
if($debug){ 
echo "delete from $table where 1=1 $sqlwhere"; 
exit; 
}elseif($execrow){ 
return $pdo->exec("delete from $table where 1=1 $sqlwhere"); 
}else{ 
$pdo->query("delete from $table where 1=1 $sqlwhere"); 
} 
} 
?> 

The comments of the parameters are clearly written. If anyone needs to know the usage method, please ask me directly.

Related articles: