How do you use "$this->db->"
I'm sorry for asking a question on something that I'm sure is easily found, but Google is of no help with special characte开发者_开发技巧rs.
I'm trying to find the best ways to work and get data from a database with PHP and I've seen people use code similar to this question: Pulling information from 2 tables
I just can't figure out what that type of code is called, and where I can learn more about it. Seems like that's what I want to do instead of writing queries over and over to achieve similar results.
That is code that utilizes the CodeIgniter MVC framework, more specifically the Active Record class for working with databases. To use syntax like that you would have to use CodeIgniter as the framework for your project.
You can go through this
Active Record Class
The work becomes really simple with this method:
For Selecting Data
$this->db->get();
$this->db->get_where();
$this->db->select();
For Inserting
$this->db->insert();
For Updating
$this->db->update();
For Deleting
$this->db->delete();
Things are as simple as these. Go through the Active Record Class and there a lot of other ways to create complex queries using the CI framework.
They use codeigniter, you can see it in that question
The above code is written upon the framework CodeIgniter and calls functions of the database class that CodeIgniter provides. CodeIgniter is an MVC framework that provides many useful and convenient classes to assist PHP application development.
The basic way to query databases (to access their data) is the PHP database functions. These are included in PHP so they require no installation. If my database were MySQL, I would use the functions mysql_connect(), mysql_close(), mysql_query(), mysql_fetch_array(), and others to retrieve and modify the data in my MySQL database.
SQL databases must be communicated with using their SQL language. The function mysql_query() lets me send a command written in SQL as a string to the database.
There are other, more advanced ways to interact with a database in PHP.
Object Relational Mapping (ORM) spares the programmer from writing SQL directly. ORM provides methods so that I may call the methods to get what I wish for. In the Flourish library, for example, if I want to get all rows of table "users" I can write: fRecordSet::build('user')
. Examples of good ORM libraries, in the order of my recommendation: Doctrine, Propel, RedBeanPHP, Flourish, Hibernate, Repose, dORM, Xyster, and Outlet.
Prepared Statements lets the programmer write an SQL statement once with empty slots, and call it repeatedly with different values for each slot. PHP comes with a prepared statement library, called Prepared Data Objects (PDO).
精彩评论