-
Define ('dsn ', 'MySQL: host = 127.0.0.1; dbname = baozhong_tour'); // database address + constant of 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 input database parameter information
- $ Db-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // you can specify a database error message.
- $ Db-> query ('set names utf8; '); // sets the encoding to utf8.
- Return $ db;
- }
- Catch (PDOException $ e ){
- Echo 'data processing error. please contact the website administrator! ';
- Print_r ($ e); // detailed error message. this sentence must be commented out after going online!
- Return false;
- }
- }
- /**
- * @ Author shuimugan
- * @ Version 0.2
- * @ Param String $ tablename table name, Type: String
- * @ Param Array $ column name of column_name, the format of the Array
- * @ Param String $ condition, for example, 'where name =? And pws =? 'Or 'name like? '
- * @ Param Array $ condition_value indicates the data corresponding to the condition. the type must be an Array, for example, 'Name =? And pws =? ,
- * @ Return Array returns an Array of result sets.
- * @ Example $ column_name = array ('*');
- $ Condition_value = array ('1 ');
- $ Limit = 'limit ';
- $ 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 an SQL statement
- Foreach ($ column_name as $ col_name ){
- // Dynamically append a column name to an SQL statement
- If ($ col_name = '*'){
- $ SQL. = '*';
- Break;
- }
- If ($ col_name = end ($ column_name ))
- {
- $ SQL. = $ col_name. ""; // if it belongs to the last one, the statement is not concatenated with a comma
- } Else {
- $ SQL. = $ col_name. ","; // splice column name + comma
- }
- }
- $ SQL. = "from". $ tablename. ""; // concatenate a table name
- $ SQL. = $ condition. ''; // concatenate a condition statement
- If (strlen ($ limit)> 0 ){
- $ SQL. = $ limit; // concatenate a limit statement
- }
- $ Db = new PDO (DSN, DB_USERNAME, DB_USERPWD); // Create a pdo object and input database parameter information
- $ Db-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // you can specify a database error message.
- $ Db-> query ('set names utf8; '); // sets the encoding to utf8.
- $ Stmt = $ db-> prepare ($ SQL );
- $ I = 1; // start counting, computing? Number
- $ J = count ($ condition_value );
- For (; $ I <= $ j ;){
- $ Stmt-> bindParam ($ I, $ condition_value [$ i-1]); // bind the parameter
- $ I ++;
- }
- // Query
- $ Stmt-> setFetchMode (PDO: FETCH_ASSOC); // you can specify a field to obtain data.
- $ Stmt-> execute ();
- // Obtain data
- Return $ stmt-> fetchAll ();
- // Return $ stmt-> rowCount ();
- } Catch (PDOException $ e ){
- Return false;
- Echo 'data processing error. please contact the website administrator! ';
- Print_r ($ e); // detailed error message. this sentence must be commented out after going online!
- }
- }
- /**
- * @ Author shuimugan
- * @ Version 0.2
- * @ Param String $ tablename table name, Type: String
- * @ Param Array $ column name of column_name, the format of the Array
- * @ Param String $ condition, for example, 'where name =? And pws =? 'Or 'name like? '
- * @ Param Array $ condition_value indicates the data corresponding to the condition. the type must be an Array, for example, 'Name =? And pws =? ,
- * @ Return int the number of returned Records
- * @ Example $ column_name = array ('*');
- $ Condition_value = array ('1 ');
- $ Limit = 'limit ';
- $ 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 an SQL statement
- Foreach ($ column_name as $ col_name ){
- // Dynamically append a column name to an SQL statement
- If ($ col_name = '*'){
- $ SQL. = '*';
- Break;
- }
- If ($ col_name = end ($ column_name ))
- {
- $ SQL. = $ col_name. ""; // if it belongs to the last one, the statement is not concatenated with a comma
- } Else {
- $ SQL. = $ col_name. ","; // splice column name + comma
- }
- }
- $ SQL. = "from". $ tablename. ""; // concatenate a table name
- $ SQL. = $ condition. ''; // concatenate a condition statement
- If (strlen ($ limit)> 0 ){
- $ SQL. = $ limit; // concatenate a limit statement
- }
- ;
- $ Db = new PDO (DSN, DB_USERNAME, DB_USERPWD); // Create a pdo object and input database parameter information
- $ Db-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // you can specify a database error message.
- $ Db-> query ('set names utf8; '); // sets the encoding to utf8.
- $ Stmt = $ db-> prepare ($ SQL );
- $ I = 1; // start counting, computing? Number
- $ J = count ($ condition_value );
- For (; $ I <= $ j ;){
- $ Stmt-> bindParam ($ I, $ condition_value [$ i-1]); // bind the parameter
- $ I ++;
- }
- // Query
- // $ Stmt-> setFetchMode (PDO: FETCH_ASSOC); // you can specify a field to obtain data.
- $ Stmt-> execute ();
- // Obtain data
- Return $ stmt-> rowCount ();
- } Catch (PDOException $ e ){
- Return false;
- Echo 'data processing error. please contact the website administrator! ';
- Print_r ($ e); // detailed error message. this sentence must be commented out after going online!
- }
- }
- /**
- * @ Author shuimugan
- * @ Version 0.2
- * @ Param String $ tablename table name, Type: String
- * @ Param Array $ column name of column_name, the format of the Array
- * @ Param Array $ column_value indicates the data corresponding to the Array format.
- * @ Return int returns the last auto-increment id.
- * @ Example $ column_name = array ('pwd', 'IP', 'name ');
- $ Column_value = array ('20140901', '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'; // Initialize an SQL statement
- $ SQL. = $ tablename. '('; // concatenate the table name
- Foreach ($ column_name as $ col_name ){
- // Dynamically append a column name to an SQL statement
- If ($ col_name = end ($ column_name ))
- {
- $ SQL. = $ col_name. ")"; // if it belongs to the last one, splice the right brackets
- } Else {
- $ SQL. = $ col_name. ","; // splice column name + comma
- }
- }
- $ SQL. = 'values ('; // concatenate $ condition_value statement
- For ($ I = 0; $ I <count ($ column_name); $ I ++) {// question mark?
- If ($ I = count ($ column_name)-1 ){
- $ SQL. = '?) ;';
- } Else {
- $ SQL. = '?, ';
- }
- }
- $ Db = new PDO (DSN, DB_USERNAME, DB_USERPWD); // Create a pdo object and input database parameter information
- $ Db-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // you can specify a database error message.
- $ Db-> query ('set names utf8; '); // sets the encoding to utf8.
- $ Stmt = $ db-> prepare ($ SQL );
- For ($ I = 1; $ I <= count ($ column_name); $ I ++) {// question mark?
- $ Stmt-> bindParam ($ I, $ column_value [$ i-1]);
- }
- // Query
- // Echo $ SQL;
- $ Stmt-> setFetchMode (PDO: FETCH_ASSOC); // you can specify a field to obtain data.
- $ Stmt-> execute ();
- // Obtain data
- Return $ db-> lastInsertId ();
- } Catch (PDOException $ e ){
- Return false;
- Echo 'data processing error. please contact the website administrator! ';
- Print_r ($ e); // detailed error message. this sentence must be commented out after going online!
- }
- }
- /**
- * @ Author shuimugan
- * @ Version 0.2
- * @ Param String $ tablename table name, Type: String
- * @ Param Array $ column name of column_name, the format of the Array
- * @ Param Array $ column_value indicates the data corresponding to the Array format.
- * @ Param String $ THE where statement String corresponding to condition is like 'Where id =? '
- * @ Param Array $ condition_value: the data type of the question mark after where must be an Array.
- * @ Return int returns the number of affected data rows.
- * @ Example $ column_name = array ('pwd', 'IP', 'name ');
- $ Column_value = array ('20140901', '192. 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 an SQL statement
- $ SQL. = $ tablename. 'set'; // list the table names.
- Foreach ($ column_name as $ col_name ){
- // Dynamically append a column name to an SQL statement
- If ($ col_name = end ($ column_name ))
- {
- $ SQL. = $ col_name. "=? "; // If it belongs to the last
- } Else {
- $ SQL. = $ col_name. "= ?, "; // Splice column names with commas (,)
- }
- }
- $ SQL. = $ condition; // concatenate a condition statement
- If (strlen ($ limit)> 0 ){
- $ SQL. = $ limit; // concatenate a limit statement
- }
- $ Db = new PDO (DSN, DB_USERNAME, DB_USERPWD); // Create a pdo object and input database parameter information
- $ Db-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // you can specify a database error message.
- $ Db-> query ('set names utf8; '); // sets the encoding to utf8.
- $ Stmt = $ db-> prepare ($ SQL); // prepare the statement
- $ I = 1;
- $ Total = count ($ column_name) + count ($ condition_value );
- For (; $ I <= count ($ column_name );){
- $ Stmt-> bindParam ($ I, $ column_value [$ i-1]); // bind the column name to the corresponding data parameter
- $ I ++;
- }
- $ J = 1;
- For (; $ I <= $ total ;){
- $ Stmt-> bindParam ($ I, $ condition_value [$ j-1]); // The Data parameter corresponding to the binding condition
- $ I ++;
- $ J ++;
- }
- // Query
- $ Stmt-> setFetchMode (PDO: FETCH_ASSOC); // you can specify a field to obtain data.
- $ Stmt-> execute ();
- // Obtain data
- Return $ stmt-> rowCount ();
- } Catch (PDOException $ e ){
- Return false;
- Echo 'data processing error. please contact the website administrator! ';
- Print_r ($ e); // detailed error message. this sentence must be commented out after going online!
- }
- }
- /**
- * @ Author shuimugan
- * @ Version 0.2
- * @ Param String $ tablename table name
- * @ Param String $ condition such as 'Where id =? '
- * @ Param Array $ the value corresponding to the condition_value condition is? Corresponding value
- * @ Param String $ limit statement, for example, 'Limit'
- * @ 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 an SQL statement
- $ SQL. = $ tablename. ''; // list the table names.
- $ SQL. = $ condition; // concatenate a condition statement
- If (strlen ($ limit)> 0 ){
- $ SQL. = $ limit; // concatenate a limit statement
- }
- $ Db = new PDO (DSN, DB_USERNAME, DB_USERPWD); // Create a pdo object and input database parameter information
- $ Db-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // you can specify a database error message.
- $ Db-> query ('set names utf8; '); // sets the encoding to utf8.
- $ Stmt = $ db-> prepare ($ SQL); // prepare the statement
- For ($ I = 1; $ I <= count ($ condition_value );){
- $ Stmt-> bindParam ($ I, $ condition_value [$ i-1]); // The Data parameter corresponding to the binding condition
- $ I ++;
- }
- // Query
- $ Stmt-> setFetchMode (PDO: FETCH_ASSOC); // you can specify a field to obtain data.
- $ Stmt-> execute ();
- // Obtain data
- Return $ stmt-> rowCount ();
- } Catch (PDOException $ e ){
- Return false;
- Echo 'data processing error. please contact the website administrator! ';
- Print_r ($ e); // detailed error message. this sentence must be commented out after going online!
-
}
- }
- ?>
|