What the PHP CI framework has learned recently

Source: Internet
Author: User
Tags pconnect

Details of the CI framework configuration considerations

In config, the database.php is related to the database configuration.

1 $db[' Default '] =Array(2' DSN ' = ',3' Hostname ' = ' 127.0.0.1,//IP4' Username ' = ' root ',//user name5' Password ' = ' root ',//password6' Database ' = ' kp_community ',//DB name7' Dbdriver ' = ' mysqli ',//database driven, if PHP7, only use mysqli8' Dbprefix ' = ' t_ ',//data table prefix, no can be empty9' Pconnect ' =FALSE,//Whether persistent connection, generally not recommended true, will affect the connection resourcesTen' Db_debug ' = (Environment!== ' production '), One' Cache_on ' =FALSE,//cache A' Cachedir ' = ', -' Char_set ' = ' utf8 ',//coded -' Dbcollat ' = ' utf8_general_ci ', the' Swap_pre ' = ', -' Encrypt ' =FALSE, -' Compress ' =FALSE, -' Stricton ' =FALSE, +' Failover ' =Array(), -' Save_queries ' =FALSE
Whether to save the query statement, the database performance is very good can be set to true, you can speed up the efficiency, general settings to see what the actual situation of the project +);
Because the CI framework is sometimes considered ' pconnect 'FALSEand ' save_queries ' =FALSEare set and will also
If the too many Contions error occurs, check to see if the number of connections is too small, and you need to involve the query in the code.
Manually add $this->db->close () later, because the disconnect mechanism of the CI framework is not immediately broken according to usage
Instead, it disconnects in a few seconds, so if you have too much access at the same time for a few seconds, the problem may occur.

Curd operation of CI

1<?PHP2 3 defined(' BasePath ') ORExit(' No Direct script access allowed ');4 5 classTest_modelextendsCi_model {6     //constructor Function7      Public function__construct () {8Parent::__construct ();9         $this->load->database ();Ten     } One      A      Public functionSelect () { -  -             //Query Operations the             $sql 1= "SELECT * from TEST WHERE ID > 1";//querying all values of id>1 -             $query 1=$this->db->query ($sql 1);//turn query statements into result sets -             $result 1=$query 1->result_array ();//Result_array () is converted to a two-dimensional array -             $count=$query 1->num_rows ();//Num_rows () is the number of rows in the query result set +  -             $sql 2= "SELECT * from TEST WHERE ID = 1";//Query the row value of Id=1 +             $query 2=$this->db->query ($sql 2);//turn query statements into result sets A             $result 2=$query 2->row_array ();//Row_array () is converted to a one-dimensional array at          - not only num_rows () to query the number of rows -             $count=$this->db->count_all_results ($sql 1);//This usage can also implement the number of rows in the query result set -  - //Query the number of rows in a table -             $tableCount=$this->db->count_all ("TEST");//querying all rows of the test table in     } -  to      Public functionInsert () { +  -           $data=Array( the"id" = 3, *"Name" = "Test Data" $               );Panax Notoginseng           //the first method, the CI Insert method -           $this->db->insert ("TEST",$data);//insert data into test table the           Echo $this->db->affected_rows ();//affect the number of rows +  A //second method, via query the           $sql= "INSERT into Test (id,name) VALUES (3, ' test data ')"; +           $this->db->query ($sql); -           Echo $this->db->affected_rows ();//affect the number of rows $     } $  -      Public functionDelete () { -  the             //the first method, the Delete method of CI -           $this->db->where (Array("id" =>1)) ->delete ("TEST");//Delete id=1 data from test tableWuyi           Echo $this->db->affected_rows ();//affect the number of rows the  - //second method, via query Wu           $sql= "DELETE from TEST WHERE id = 1"; -           $this->db->query ($sql); About           Echo $this->db->affected_rows ();//affect the number of rows $     } -  -        Public functionUpdate () { -  A             $newdata=Array( +"Name" = "New test Data" the                 ); -             //the first method, the CI Update method $           $this->db->where (Array("id" =>3)) ->update ("TEST",$newdata);//the id=3 data in the test table is updated to NewData the           Echo $this->db->affected_rows ();//affect the number of rows the  the //second method, via query the           $sql= "UPDATE Test SET name = ' New testing data ' WHERE ID = 3"; -           $this->db->query ($sql); in           Echo $this->db->affected_rows ();//affect the number of rows the     }   the  About      Public functionTrans () { the         $this->db->Trans_begin (); the         /* the a large string of curd data operations +         */ -         if($this->db->trans_status () = = =FALSE ) { the             Bayi             $this->db->trans_rollback ();//Transaction Rollback the             $this->db->close (); the             return-1; -}Else { -              the             $this->db->trans_commit ();//Transaction Commit the             $this->db->close (); the             return1; the         } -     }   the  the}

The JSON output format of the CI framework

1<?PHP2 3 defined(' BasePath ') ORExit(' No Direct script access allowed ');4 error_reporting(E_error|e_warning|E_parse);5 6 classTestextendsCi_controller {7 8     ConstSTATUS = 1998;//if the session fails after the status code9     ConstWyid = ' Com_wyid ';//Property IDTen     ConstCODE = ' Com_code ';//REDIRECT Single Sign-on interface return code, valid for 2 minutes, after use once failed One     ConstAccesstoken = ' Com_accesstoken ';//access credentials that are obtained A //const Wyid = ' Com_wyid ';//property ID -     ConstLogininfo = ' Com_info ';//sign-in judging conditions - //Construction method the      Public function__construct () { -Parent::__construct (); -         //Load Model -         $this->load->model (' Test_model '); +     $this->load->library (' Session ')); -     } +      A      Public functionTest () { at$arr = Array ("id" =>1, "name" = "My Name"); -          //output in JSON format -         $this-Output -Set_content_type (' Application/json ')  -Set_output (Json_encode ($arr)); -     } in } -  to  +?>

What the PHP CI framework has learned recently

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.