溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在php中使用kohana框架連接數(shù)據(jù)庫

發(fā)布時間:2021-04-23 16:16:22 來源:億速云 閱讀:187 作者:Leah 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)怎么在php中使用kohana框架連接數(shù)據(jù)庫,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

php是什么語言

php,一個嵌套的縮寫名稱,是英文超級文本預(yù)處理語言(PHP:Hypertext Preprocessor)的縮寫。PHP 是一種 HTML 內(nèi)嵌式的語言,PHP與微軟的ASP頗有幾分相似,都是一種在服務(wù)器端執(zhí)行的嵌入HTML文檔的腳本語言,語言的風(fēng)格有類似于C語言,現(xiàn)在被很多的網(wǎng)站編程人員廣泛的運用。

1、添加database支持。在kohana\application\bootstrap.php下找到如下段

Kohana::modules(array(
// 'auth'       => MODPATH.'auth',       // Basic authentication
// 'cache'      => MODPATH.'cache',      // Caching with multiple backends
// 'codebench'  => MODPATH.'codebench',  // Benchmarking tool
// 'database'   => MODPATH.'database',   // Database access
// 'image'      => MODPATH.'image',      // Image manipulation
// 'orm'        => MODPATH.'orm',        // Object Relationship Mapping
// 'unittest'   => MODPATH.'unittest',   // Unit testing
// 'userguide'  => MODPATH.'userguide',  // User guide and API documentation
));

去掉database前面的注釋。

2、在kohana\application\config下添加database.conf配置文件,內(nèi)容如下

<?php defined('SYSPATH') or die('No direct access allowed.');
return array
(
    'default' => array
    (
        'type'       => 'mysql',
        'connection' => array(
            'hostname'   => 'localhost',
            'username'   => 'root',
            'password'   => 'password',
            'persistent' => FALSE,
            'database'   => 'kohanademo',
        ),
        'table_prefix' => '',
        'charset'      => 'utf8',
        'profiling'    => TRUE,
    ),
);
 ?>

修改對應(yīng)的數(shù)據(jù)庫名和密碼就好。

3、在代碼中添加數(shù)據(jù)庫查詢段,kohana\application\classes\controller\user\user.php內(nèi)容修改如下

<?php defined('SYSPATH') or die('No direct script access.');
 
class Controller_User_User extends Controller {
 
public function action_index()
{
$username = Session::instance()->get('username');
$this->response->body('logined:'.$username);
}
 
public function action_login()
{
if($this->request->post())
{
$username = $this->request->post('username');
$password = $this->request->post('password');
 
$query = DB::query(Database::SELECT, "SELECT username FROM user WHERE username=:username AND password=:password");
$query->param(':username', $username);
$query->param(':password', $password);
$user = $query->execute()->current();
if(count($user) > 0)
{
Session::instance()->set('username',$username);
}
$this->request->redirect('/user/user/index');
}
}
}
 ?>

關(guān)于怎么在php中使用kohana框架連接數(shù)據(jù)庫就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI