In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to use codeigniter's own database class". In daily operation, I believe many people have doubts about how to use codeigniter's own database class. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use codeigniter's own database class". Next, please follow the editor to study!
Initialize the database class
Load and initialize database classes based on your database configuration:
The copy code is as follows:
This- > load- > database ()
You can use it anywhere after it is loaded.
Returns the query result in the form of an object
The copy code is as follows:
$query = $this- > db- > query ('SELECT name, title, email FROM my_table')
Foreach ($query- > result () as $row)
{
Echo $row- > title
Echo $row- > name
Echo $row- > email
}
Echo 'Total Results:' $query- > num_rows ()
The above result () function returns an array of objects. For example: $row- > title
Returns the query results as an array
The copy code is as follows:
$query = $this- > db- > query ('SELECT name, title, email FROM my_table')
Foreach ($query- > result_array () as $row)
{
Echo $row ['title']
Echo $row ['name']
Echo $row ['email']
}
The result_array () function above returns an array with an index. For example: $row ['title']
Return a piece of data
Object form:
The copy code is as follows:
$query = $this- > db- > query ('SELECT name FROM my_table LIMIT 1')
$row = $query- > row ()
Echo $row- > name
The above row () function returns an object. For example: $row- > name
Array form:
The copy code is as follows:
$query = $this- > db- > query ('SELECT name FROM my_table LIMIT 1')
$row = $query- > row_array ()
Echo $row ['name']
The above row_array () function returns an array. For example: $row ['name']
Insert (insert) data
The copy code is as follows:
$sql = "INSERT INTO mytable (title, name)
VALUES (". $this- > db- > escape ($title).", ". $this- > db- > escape ($name).") "
$this- > db- > query ($sql)
Echo $this- > db- > affected_rows ()
Database configuration
CodeIgniter has a configuration file that allows you to store database connection values (username: user name, password: password, database name: database name, etc.). The configuration file is located in the following path: application/config/database.php
The accessories file is stored in a multi-dimensional array in the following format:
The copy code is as follows:
$db ['default'] [' hostname'] = "localhost"
$db ['default'] [' username'] = "root"
$db ['default'] [' password'] = ""
$db ['default'] [' database'] = "database_name"
$db ['default'] [' dbdriver'] = "mysql"
$db ['default'] [' dbprefix'] = ""
$db ['default'] [' pconnect'] = TRUE
$db ['default'] [' db_debug'] = FALSE
$db ['default'] [' cache_on'] = FALSE
$db ['default'] [' cachedir'] = ""
$db ['default'] [' char_set'] = "utf8"
$db ['default'] [' dbcollat'] = "utf8_general_ci"
The reason for using multidimensional arrays is to allow you to store settings for multiple connection values at will. For example: if you run multiple environments (development: development, production: production, test: testing, etc.), you can establish separate connection groups for each environment and switch directly within the groups. For example, to set up a "test" environment, you can do this:
The copy code is as follows:
$db ['test'] [' hostname'] = "localhost"
$db ['test'] [' username'] = "root"
$db ['test'] [' password'] = ""
$db ['test'] [' database'] = "database_name"
$db ['test'] [' dbdriver'] = "mysql"
$db ['test'] [' dbprefix'] = ""
$db ['test'] [' pconnect'] = TRUE
$db ['test'] [' db_debug'] = FALSE
$db ['test'] [' cache_on'] = FALSE
$db ['test'] [' cachedir'] = ""
$db ['test'] [' char_set'] = "utf8"
$db ['test'] [' dbcollat'] = "utf8_general_ci"
So, tell the system to use the "test" group, and you can set the variables located in the configuration file:
The copy code is as follows:
$active_group = "test"
Note: the name "test" is arbitrary, which allows you to set it freely, our main connection uses the name "default" by default, of course, you can give it a more meaningful name based on your project.
Active Record
The Active Record class can be set globally (allow / disable TRUE/FALSE (boolean)) through the $active_record variable in the database configuration file. If you don't use this class, you can reduce the consumption of computer resources during database class initialization by setting the value of this variable to FALSE. $active_record = TRUE
Note: some CodeIgniter classes, such as Sessions, need Active Records support when executing some functions.
Parameter resolution:
Hostname-the hostname of the database, usually on the local machine, which can be represented as "localhost".
Username-the user name that needs to connect to the database.
Password-the password to log in to the database.
Database-the name of the database to which you need to connect.
Dbdriver-Database type. Such as: mysql, postgres, odbc and so on. Must be lowercase letters.
Dbprefix-the prefix of the data table when running an Active Record query, which allows multiple CodeIgniter programs to be installed on a database.
Pconnect-TRUE/FALSE (boolean)-use persistent connections.
Db_debug-TRUE/FALSE (boolean)-displays a database error message.
Cache_on-TRUE/FALSE (boolean)-the database query cache is enabled. For more information, please see the database cache class.
Cachedir-the absolute path to the server where the database query cache directory is located.
Char_set-the character set used when communicating with the database.
Dbcollat-character rules (character collation) used when communicating with the database.
Port-Database port number. Currently it is only used for Postgres drivers. To use this value, you should add a line of code to the database configuration array.
At this point, the study on "how to use codeigniter's own database class" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.