Kohana Database Usage

Source: Internet
Author: User
Tags dsn prev kohana

But the official website does not use the tutorial, oneself groped out, has the wrong time everybody points out to Oh, the common progress ~

First is the configuration: modules\database\config\database.php

<?php ' default ' = = Array (' type '       = ' pdo ', ' connection ' = = Array (/** * The following options are available fo R PDO: * * string   DSN         data Source Name * String   username    Database username * String   password    data Base Password * Boolean  persistent use  persistent connections? */' DSN '        = ' mysql:host=localhost;dbname =kohana ', ' username '   = ' ****** ',//' password '   = ' ****** ',//' persistent ' = FALSE,/** * the Following extra options is available for PDO: * * String   identifier  set the escaping identifier */' Table_prefix ' = ' = ' ko_ ', ' charset '      = ' utf8 ', ' caching ' and '      = FALSE ', ' Profiling ' and '    = TRUE ',


Multiple database configurations can be configured OH ~

After the configuration is ready, you can use it.

You can use it when you've finished configuring

You can do that in your controller.

DB instance

There are two types of database instances

1.DB DB Instance

2. Database DB Instance

Where DB is the re-encapsulation of database

The following describes the use of two DB instances

Database

Method of Acquisition:

<? Php$database=database::instance (); //Can get database instance #例外在模型中, Dababase is the only pass parameter for the model constructor and has $this->_db property in the model $database = $this->_db;
How to use: (assuming in the model)
Insert data:
<? Php
$sql = "INSERT into ' kohana '. ' User ' (' name ', ' age ') VALUES (' Test ', ' 1 '), (' Test2 ', ' 2 ')"; $dat = $this->_db-> ; Query (Database::insert, $sql, false);
Update data:
<? Php$sql= "UPDATE ' ko_users ' SET ' user_name ' = ' test111q1 '  "; $dat = $this->_db->query (Database:: UPDATE, $sql, false); #return Returns the number of affected rows

Delete data:
<? Php$sql= "DELETE from ' Kohana '. ' User ' WHERE ' user '. ' id ' = 1"; $dat = $this->_db->query (Database::D elete,$ Sql,false); #return Returns the number of affected rows
Query data:
<? Php$sql= "select * from Ko_com_var"; $res = $this->_db->query (Database::select, $sql, false); #得到所有查询数据$res->as_array (); #得到一条查询结果$res->offsetget (0); #取得特定记录的指定字段值$res->get ("name"); #移动指针并取得指定字段$res->next ()->get ("name"), $res->prev ()->get ("name"); #计算取得结果总数$res->count (); #还有其他方法不在一一罗列, please view the manual

Other common helper functions:
<? PHP#过滤字符串用, do not know why put in this single case, it is common to ~, perhaps the filter of each database of things have a difference $str = $this->_db->escape ("ddddd  DDD "); #表前缀, this commonly used ~$str = $this->_db->table_prefix (); #还有其他查看帮助, not introduced.

DB instances are used (as demonstrated in the Kohana environment) in two ways: the first: The following execute (); There is a database adapter parameter that specifies the operation of the database when there are multiple data connections, which is the key value of the configuration file that inserts the data:
<? Php$sql= "INSERT into ' kohana '. ' User ' (' name ', ' age ') VALUES (' Test ', ' 1 '), (' Test2 ', ' 2 ')"; #其实也可以用Database:: UPDATE, the result only returns the number of rows affected, but still according to the specification good. Hehe ~, the above Database can also $dat =db::query (Database::insert, $sql); $ row= $dat->execute (); #返回的两个值中, the first one is an auto-growing ID, and if so, the second is the number of rows affected Kohana::d ebug ($row);

Data Update:
<? Php$sql= ""; $dat =db::query (Database::update, $sql); $row = $dat->execute (); #返回影响行数echo Kohana::d ebug ($row);
Data deletion:
<? Php$sql= "DELETE from ' Kohana '. ' User ' WHERE ' user '. ' id ' = 1"; $dat =db::query (Database::D elete, $sql); $row =$ Dat->execute (); #返回影响行数echo Kohana::d ebug ($row);

Data query:
 <?  Php$sql= "select * from user "; $dat =db::query (Database::select, $sql);  #指定数据库取数据  $row = $dat->execute ($database)->offsetget (0);  #默认数据库取数据, like the above database, returns the Database_result_cached object, implements the iterator pattern  $rus = $dat- >execute ();  #取得部分结果  $row = $rus->offsetget (0);  #取得所有结果  $allrow = $rus->as_array ();  #取得特定记录的指定字段值  $no 1name= $rus->get ("name  ");  #移动数组指针 and takes the specified field value  $no 2name= $rus->next ()->get (" name  ");  #当前指针  echo $rus->key ();  #移动数组指针, and takes the specified field value  echo $no 1name= $rus->prev ()->get (' name  ');  #取行数  echo $rus->count (); 

The second: (The official website of the document called the query mode, not good, hehe, simple can be used) Insert data:
<? Php$query = Db::insert ('user', array ('user', 'Age '))->values (Array ('test1', 'One ')); $query->execute (); #返回和上面一样

Update data:
<? Php$query = db::update ('user')->set (Array ('age ' = ') ')->where ('user ', '=', 'test1'); $query->execute (); #返回和上面一样


Delete data:

<? Php$query = DB::d elete ('user')->where ('age ', ' in ', Array ('+ ', ' one '); $query->execute (); #返回和上面一样


Query data:

<? Php$query = Db::select ()->from ('user')->where ("ID", "=", "1"); $res =$ Query->execute (); #和上面一样, $res is the Database_result_cached object $res->as_array (); #其他方法不演示了 ~

Note:
Data binding, copy of the official example of a, it is very simple, easier to read
<? Php$query = Db::query (Database::insert, 'INSERT into users (username, password) VALUES (: User,:p)')      -& Gt;bind (': User', $username)    ->bind (':p", $password);  foreach ($new _users as $username = + $password) {    $query->execute ();}

Basically have, and I do not commonly used, not introduced, but basically all introduced,

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.