codeigniterの$ this-> db-> query()と$ this-> db-> select()の違いは何ですか?

In CodeIgniter, in both $this->db->query() and $this->db->select() we can select the rows from the table. can anyone summarize the difference briefly?

We can run any queries by using $this->db->query(). Then whats the use of $this->db->select()?? why we need it??

追加された 著者 thomaux,
追加された 著者 Melvin Mathews,

4 答え

Without knowing CodeIgniter at all, the name gives it all away. With $this->db->select() you build and perform SELECT queries. It would make sense if similiar methods are available, like $this->db->update() and $this->db->delete().

$this->db->query looks like it is fit for a custom made query (which you shouldn't, when this is available)


Looking at the docs, it is now clear that the method $this->db->select is a integral part of query-building and chaining, where you can use a clever system to select your data without knowing any SQL, where you actually have to escape and build your query with using $this->db->query


$this->db->select() just builds a part of the query; it belongs the the Active Record class, in fact, and using it alone doesn't make sense because it just creates the "SELECT field,field1,field3" part, nothing else.
You need all the other parts to build a full query, and the call the ->get() method and, on that, retrieve the results.

$this->db->query() is just a function helper that make you run the query you pass it as an argument.

Important difference: Active Record automatically escapes queries, while $this->db->query() doesn't, so you should call $this->db->escape() on individual variables or run it using placeholders:

$sql = "INSERT INTO table (field1,field2) VALUES (?,?)";
$query = $this->db->query($sql,array('asd','lol'));


$this->db->query('YOUR QUERY HERE');





Using the $this->db->select() offers you simplicity in writing queries. You can always write the query manually and execute it using $this->db->query(), but it will be a tedious task. So instead, you can use the Active Record class that will do the magic for you. Plus, it will also do the escaping for you. When you're using Active Record, CodeIgniter will eventually execute the query using $this->db->query() when you tell it to get the data using something like $this->db->get().

See the first two paragraphs from this page for some other benefits of using Active Record

PHP - 日本のコミュニティ [ja]
PHP - 日本のコミュニティ [ja]
4 参加者の

このグループではPHPについて話します。 パートナー