Home  >  Article  >  Backend Development  >  Summary of optimized writing methods for operating database tables with Codeigniter_PHP tutorial

Summary of optimized writing methods for operating database tables with Codeigniter_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:32829browse

I have been using codeigniter for a while, but I haven’t made any summary yet. Now I will summarize some optimized writing methods for operating database tables in Codeigniter. Although it is not comprehensive, it can indeed help students who are just getting started with CI.

Link database

Copy code The code is as follows:
$this->load->database();//Manually connect to the database
//Connect multiple databases
$DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);

Query

Copy code The code is as follows:
//Parameter binding form
$sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";
$this->db->query($sql, array(3, 'live', 'Rick'));

//Multiple Result standard query
$query = $this->db->query($sql); //Customized
$query = $this->db->get('tablename'); //Convenient form, equivalent to: SELECT * FROM tablename
$query = $this->db->get('tablename', 10, 20); // Equivalent to: SELECT * FROM tablename LIMIT 20, 10

$query->result() //Object form
$query->result_array() //Array form
/*
foreach ($query->result () as $row)
{
echo $row->title;
echo $row->name;
echo $row->email;
}
*/
$query->num_rows() //Total number of items
$query->num_fields() //Number of fields

//Single result standard query
$row = $query->row(); //Object form
$row = $query->row_array(); //Array form
/*
$row = $query->row_array ();
echo $row['name'];
*/

Insert

Copy code The code is as follows:
$data = array(
             'title' => name' => $name
);
$this->db->insert('tablename', $data); //Convenient insertion of
$this->db-> insert_string('tablename', $data); //Convenient insertion

$this->db->insert_id() //The id just inserted
$this->db-> affected_rows() //Number of affected rows (update,insert)

Update

Copy code The code is as follows: email' => $email );
$where = "id = 1";
$this->db->update('tablename', $data);
$ this->db->update_string('tablename', $data, $where);



Delete

Copy code

The code is as follows:

$array = array(
                'name' => $name,
                'title' => $title
                );
$this->db->delete('tablename', $array);

// Produces:
// "DELETE FROM tablename WHERE name = '$name' AND title = "$title""

$this->db->truncate('tablename'); //清空表
// Produce: TRUNCATE tablename

 

-----------------------------------------------------
(where)
-------

$array = array(
                'name' => $name,
                'title' => $title
                );
$this->db->where($array);
// Produces: "WHERE name = '$name' AND title = "$title""
-----------------------------------------------------
$this->db->count_all('tablename'); //表中记录总行数
-----------------------------------------------------
$query->free_result() //释放资源

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/788619.htmlTechArticle用codeigniter也有一段时间了,一直没有做什么总结。现在总结一些Codeigniter操作数据库表的优化写法,虽说不全,但是也确实可以帮助那些刚...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn