Zend Framework チュートリアル: データベースに接続し、追加および削除クエリを実行する方法
この記事の例では、データベースに接続し、追加、削除、クエリを実行する Zend Framework チュートリアルの方法について説明します。参考までに皆さんと共有してください。詳細は次のとおりです。
まず、データベースに message というテーブルを作成する必要があります。このテーブルには、id、title、content の 3 つのフィールドがあります。そのうち、id が主要です。
ここで最初のステップを開始します。ステップ: アプリケーション フォルダーの下に config.ini ファイルを追加します。これには、構成データベースの基本情報が含まれています。
次のコードが表示されます。
[general] db.adapter=PDO_MYSQL //请开启PDO扩展 db.config.host=localhost //Mysql主机 db.config.username=root //用户名 db.config.password= //密码,我这里为空 db.config.dbname=zendoophp //数据库名
ログイン後にコピー
<?php class Message extends Zend_Db_Table { protected $_name ="message"; protected $_primary = 'id'; }
ログイン後にコピー
//配置数据库参数,并连接数据库 $config=new Zend_Config_Ini('./application/config/config.ini',null, true); Zend_Registry::set('config',$config); $dbAdapter=Zend_Db::factory($config->general->db->adapter, $config->general->db->config->toArray()); $dbAdapter->query('SET NAMES UTF8'); Zend_Db_Table::setDefaultAdapter($dbAdapter); Zend_Registry::set('dbAdapter',$dbAdapter);
ログイン後にコピー
class IndexController extends Zend_Controller_Action { function init() { $this->registry = Zend_Registry::getInstance(); $this->view = $this->registry['view']; $this->view->baseUrl = $this->_request->getBaseUrl(); } function indexAction() { $message=new message();//实例化数据库类 //这里给变量赋值,在index.phtml模板里显示 $this->view->bodyTitle = 'Hello World!'; //取到所有数据.二维数组 $this->view->messages=$message->fetchAll()->toArray(); //print_r( $this->view->messages); echo $this->view->render('index.phtml');//显示模版 } function addAction(){ //如果是POST过来的值.就增加.否则就显示增加页面 if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){ //过滤一些数据.不过这里还有检测一些动作没有做.. //请大家加了..我就不多写那么多了.时间关系.. Zend_Loader::loadClass('Zend_Filter_StripTags'); $filter=new Zend_Filter_StripTags(); $content=$filter->filter(($this->_request->getPost('content'))); $title=$filter->filter(($this->_request->getPost('title'))); $message=new Message(); $data=array( 'content'=>$content, 'title'=>$title ); $message->insert($data); unset($data); echo '您增加数据成功!请您 $this->view->baseUrl.'/index/index/">返回'; }else{ echo $this->view->render('add.phtml');//显示增加模版 } } public function editAction(){ $message=new Message(); $db = $message->getAdapter(); Zend_Loader::loadClass('Zend_Filter_StripTags'); $filter=new Zend_Filter_StripTags(); //同上面addAction if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){ $content=$filter->filter(($this->_request->getPost('content'))); $title=$filter->filter(($this->_request->getPost('title'))); $id=$filter->filter(($this->_request->getPost('id'))); $set=array( 'content'=>$content, 'title'=>$title ); $where = $db->quoteInto('id = ?', $id); //更新表数据 $message->update($set, $where) unset($set); echo '您修改数据成功!请您 $this->view->baseUrl.'/index/index/">返回'; }else{ $id=$filter->filter(($this->_request->getParam('id'))); $this->view->messages=$message->fetchAll('id='.$id)->toArray(); echo $this->view->render('edit.phtml');//显示编辑模版 } } public function delAction() { $message=new Message(); //能过ID删除数据.这里有一些动作没有做.比如说没有ID页面要去哪里. //.我只是给大家一个思想..所以不会那么完整 $id = (int)$this->_request->getParam('id'); if ($id > 0) { $where = 'id = ' . $id; $message->delete($where); } echo '您删除数据成功!请您 $this->view->baseUrl.'/index/index/">返回'; } }
ログイン後にコピー
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事
アサシンのクリードシャドウズ:シーシェルリドルソリューション
3週間前
By DDD
Windows11 KB5054979の新しいものと更新の問題を修正する方法
2週間前
By DDD
Atomfallのクレーンコントロールキーカードを見つける場所
3週間前
By DDD
R.E.P.O.で節約説明した(そしてファイルを保存)
1 か月前
By 尊渡假赌尊渡假赌尊渡假赌
アサシンクリードシャドウ - 鍛冶屋を見つけて武器と鎧のカスタマイズを解除する方法
4週間前
By DDD

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック
Gmailメールのログイン入り口はどこですか?
7563
15


CakePHP チュートリアル
1385
52


Steamのアカウント名の形式は何ですか
84
11


NYTの接続はヒントと回答です
28
99

