-
-
define('DSN', 'mysql:host=127.0.0.1;dbname=baozhong_tour'); //Constant of database address + database name - define('DB_USERNAME ', 'root'); //Database username
- define('DB_USERPWD', ''); //Database password
- ?>
- /**
- * @author shuimugan
- * @version 0.2
- * @return PDOStatement
- */
- function getConn() {
- try {
- $db = new PDO(DSN, DB_USERNAME, DB_USERPWD); //Create a pdo object and pass in the database parameter information
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Set database error information
- $db->query('set names utf8;'); //Set encoding to utf8
- return $db;
- }
- catch (PDOException $e) {
- echo 'Data processing error, please contact Website administrator!';
- print_r($e);//Super detailed error message, this sentence must be commented after going online!
- return false;
- }
- }
- /**
- * @author shuimugan
- * @version 0.2
- * @param String $tablename table name, type is string
- * @param Array $column_name column name, the format of the array is required
- * @param String $condition condition, such as 'where name=? and pws=?' or 'name like?'
- * @param Array $condition_value The data corresponding to the condition, the type is required to be an array, such as 'name=? and pws=?',
- * @return Array returns an Result set array format
- * @example $column_name=array('*');
- $condition_value=array('1');
- $limit='limit 0,10';
- $res=easy_select('user', $column_name, 'where id=? ', $condition_value,$limit);
- */
- function easy_select( $tablename,$column_name,$condition,$condition_value,$limit) {
- try {
- $db=getConn();
- $sql='select ';//Initialize sql statement
- foreach ($column_name as $col_name) {
- //Dynamicly append column names into sql statements
- if($col_name=='*'){
- $sql.='*';
- break;
- }
- if($col_name==end($column_name))
- {
- $sql.=$col_name." ";//If it is the last one, the statement does not splice comma
- }else {
- $sql.=$col_name.", ";//Splice column name + comma
- }
- }
- $sql.="from ".$tablename." ";//Splicing table name
- $sql.=$condition.' ';//Splicing conditional statement
- if(strlen($limit)>0){
- $sql.=$limit;//Splice the limit statement
- }
- $db = new PDO(DSN, DB_USERNAME, DB_USERPWD);//Create a pdo object and pass in the database parameter information
- $db->setAttribute(PDO:: ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//Set database error information
- $db->query('set names utf8;');//Set encoding to utf8
- $stmt = $db->prepare($sql) ;
- $i=1;//Start counting, calculate the number of?
- $j=count($condition_value);
- for (; $i <= $j; ) {
- $stmt->bindParam($i , $condition_value[$i-1]);//Bind parameters
- $i++;
- }
- //Query
- $stmt->setFetchMode(PDO::FETCH_ASSOC);//Set to get data through fields
- $stmt ->execute();
- //Get data
- return $stmt->fetchAll();
- //return $stmt->rowCount();
- } catch (PDOException $e) {
- return false;
- echo 'Data processing error, please contact the website administrator!';
- print_r($e);//Super detailed error message, this sentence must be commented after going online!
- }
- }
- /**
- * @author shuimugan
- * @version 0.2
- * @param String $tablename table name, type is string
- * @param Array $column_name column name, the format of the array is required
- * @param String $condition condition, such as 'where name=? and pws=?' or 'name like?'
- * @param Array $condition_value The data corresponding to the condition, the type is required to be an array, such as 'name=? and pws=?',
- * @return int returns the record Number of items
- * @example $column_name=array('*');
- $condition_value=array('1');
- $limit='limit 0,10';
- $res=easy_selectCount('user', $column_name , 'where id=? ', $condition_value,$limit);
- */
- function easy_selectCount($tablename,$column_name,$condition,$condition_value,$limit) {
- try {
- $db=getConn();
- $sql='select ';//Initialize sql statement
- foreach ($column_name as $col_name) {
- //Dynamicly append column names into sql statements
- if($col_name=='*'){
- $sql.='*';
- break;
- }
- if($col_name==end($ column_name))
- {
- $sql.=$col_name." ";//If it belongs to the last one, the statement does not splice the comma
- }else {
- $sql.=$col_name.", ";//Splice the column name + comma
- }
- }
- $sql.="from ".$tablename." ";//Splicing table name
- $sql.=$condition.' ';//Splicing conditional statement
- if(strlen($limit)> 0){
- $sql.=$limit;//Splicing limit statement
- }
- ;
- $db = new PDO(DSN, DB_USERNAME, DB_USERPWD);//Create a pdo object and pass in the database parameter information
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//Set the database Error message
- $db->query('set names utf8;');//Set encoding to utf8
- $stmt = $db->prepare($sql);
- $i=1;//Start counting, Calculate the number of
- $j=count($condition_value);
- for (; $i <= $j; ) {
- $stmt->bindParam($i, $condition_value[$i-1]);/ / Bind parameters
- $i++;
- }
- // Query
- //$stmt->setFetchMode(PDO::FETCH_ASSOC);//Set to get data through fields
- $stmt->execute();
- // Get data
- return $stmt->rowCount();
- } catch (PDOException $e) {
- return false;
- echo 'Data processing error, please contact the website administrator!';
- print_r($e);// Super detailed error message, you must comment this sentence after going online!
- }
- }
- /**
- * @author shuimugan
- * @version 0.2
- * @param String $tablename table name, type is string
- * @param Array $column_name column name, the format of the array is required
- * @param Array $column_value corresponding data, requirements The format of the array
- * @return int returns the last incremented id
- * @example $column_name=array('pwd','ip','name');
- $column_value=array('1246','11.11.11.11 ','Zhang San');
- echo easy_insert('user',$column_name,$column_value);
- */
- function easy_insert($tablename,$column_name,$column_value) {
- try {
- $db=getConn( );
- $sql='INSERT INTO ';//Initialize sql statement
- $sql.=$tablename.' (';//Splice table name
- foreach ($column_name as $col_name) {
- //Dynamicly append column name Enter the sql statement
- if($col_name==end($column_name))
- {
- $sql.=$col_name." )";//If it is the last one, splice the right bracket
- }else {
- $sql.=$ col_name.", ";//Splicing column name + comma
- }
- }
- $sql.=' VALUES (';//Splicing $condition_value statement
- for ($i=0; $i < count($column_name) ; $i++) {//Splicing question marks?
- if ($i==count($column_name)-1) {
- $sql.='?) ;';
- }else {
- $sql.='?,' ;
- }
- }
- $db = new PDO(DSN, DB_USERNAME, DB_USERPWD);//Create a pdo object and pass in the database parameter information
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);/ /Set database error information
- $db->query('set names utf8;');//Set encoding to utf8
- $stmt = $db->prepare($sql);
- for ($i=1; $i <= count($column_name); $i++) {//Splicing question marks?
- $stmt->bindParam($i, $column_value[$i-1]);
- }
- // Query
- // echo $sql;
- $stmt->setFetchMode(PDO::FETCH_ASSOC);//Set to get data through fields
- $stmt->execute();
- //Get data
- return $db->lastInsertId() ;
- } catch (PDOException $e) {
- return false;
- echo 'Data processing error, please contact the website administrator!';
- print_r($e);//Super detailed error message, this sentence must be commented after going online Words!
- }
- }
- /**
- * @author shuimugan
- * @version 0.2
- * @param String $tablename table name, type is string
- * @param Array $column_name column name, the format of the array is required
- * @param Array $column_value corresponding data, requirements The format of the array
- * @param String $condition corresponds to the where statement string such as 'where id=?'
- * @param Array $condition_value corresponds to where The data type of the question mark behind is required to be an array
- * @return int Returns the affected data Number of rows
- * @example $column_name=array('pwd','ip','name');
- $column_value=array('123456','127.152.123.132','Zhang San');
- $condition_value= array('1');
- easy_update('user',$column_name,$column_value,'where id=?',$condition_value,null);
- */
- function easy_update($tablename,$column_name,$column_value,$condition,$condition_value,$limit) {
- try {
- $db=getConn();
- $sql='UPDATE ';//Initialize the sql statement
- $sql.=$tablename.' SET ';//Splice the table name
- foreach ($column_name as $col_name) {
- //Dynamicly append the column name into the sql statement
- if($col_name==end($column_name))
- {
- $sql.=$col_name." = ? ";//If it belongs to the last
- }else {
- $sql.=$col_name." = ?, " ;//Splicing column name + comma
- }
- }
- $sql.=$condition;//Splicing conditional statement
- if(strlen($limit)>0){
- $sql.=$limit;//Splicing limit Statement
- }
- $db = new PDO(DSN, DB_USERNAME, DB_USERPWD);//Create a pdo object and pass in the database parameter information
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//Set Database error message
- $db->query('set names utf8;');//Set encoding to utf8
- $stmt = $db->prepare($sql);//Prepare statement
- $i=1;
- $total=count($column_name)+count($condition_value);
- for (;$i <= count($column_name);) {
- $stmt->bindParam($i, $column_value[$i- 1]);//Bind the data parameter corresponding to the column name
- $i++;
- }
- $j=1;
- for (;$i <= $total;) {
- $stmt->bindParam($i, $condition_value[$j-1]);//The data parameters corresponding to the binding conditions
- $i++;
- $j++;
- }
- //Query
- $stmt->setFetchMode(PDO::FETCH_ASSOC);//Set to get data through fields
- $stmt->execute();
- //Get data
- return $stmt->rowCount() ;
- } catch (PDOException $e) {
- return false;
- echo 'Data processing error, please contact the website administrator!';
- print_r($e);//Super detailed error message, this sentence must be commented after going online Words!
- }
- }
- /**
- * @author shuimugan
- * @version 0.2
- * @param String $tablename table name
- * @param String $condition conditions such as 'where id= ?'
- * @param Array $condition_value The value corresponding to the condition is the value corresponding to ?
- * @param String $limit limit statement such as 'limit 0,1'
- * @return int Returns the number of affected results
- * @example $condition_value=array('83');
- echo easy_delete('user', 'where id =?', $condition_value, null);
- */
- function easy_delete($tablename,$condition,$condition_value,$limit) {
- try {
- $db=getConn();
- $sql='DELETE FROM ';//Initialize sql statement
- $sql.=$tablename.' ';//Splice table name
- $sql.=$condition;//Splice conditional statement
- if(strlen($limit)>0){
- $sql.=$limit;//Splice the limit statement
- }
- $db = new PDO(DSN, DB_USERNAME, DB_USERPWD);//Create a pdo object and pass in the database parameter information
- $db->setAttribute(PDO:: ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//Set database error information
- $db->query('set names utf8;');//Set encoding to utf8
- $stmt = $db->prepare($sql) ;//Prepare statement
- for ($i=1;$i <= count($condition_value);) {
- $stmt->bindParam($i, $condition_value[$i-1]);//Bind Data parameters corresponding to certain conditions
- $i++;
- }
- // Query
- $stmt->setFetchMode(PDO::FETCH_ASSOC);//Set to obtain data through fields
- $stmt->execute();
- // Get Data
- return $stmt->rowCount();
- } catch (PDOException $e) {
- return false;
- echo 'Data processing error, please contact the website administrator!';
- print_r($e);//Super Detailed error message, you must comment this sentence after going online!
}
- }
- ?>
-
Copy the code
|