サンプル集 |
■VC ■C# ■Java ■BorlandC ■LinuxC ■MS-DOS ■bash ■Excel VBA ■VBScript ■PHP ■HTML ■perl ■iPhone ■Android ■Lua ■WordPress ■PowerShell ■other |
Linuxコマンド |
用語集 |
debian メモ |
apache2 メモ |
MySQL メモ |
Oracle メモ |
HOME |
CodeIgniterでMySQLのテーブルの内容を表示してみます。
◆環境OS | Windows 10 Home 21H2 (64bit) |
---|---|
XAMPP | v3.3.0 |
MySQLへログインしデータベースとテーブルを作成します。
XAMPPでApacheのほかにMySQLも起動します。
起動したら右側の「Shell」ボタンを押します。
Setting environment for using XAMPP for Windows.
ymlib@DESKTOP-55UPABF c:\xampp
#
ymlib@DESKTOP-55UPABF c:\xampp
#
MySQLへログインします。デフォルトではrootユーザーにパスワードの設定はなかったです。
# mysql -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 22
Server version: 10.4.21-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input s
tatement.
MariaDB [(none)]>
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 22
Server version: 10.4.21-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input s
tatement.
MariaDB [(none)]>
CREATE DATABASEでmydbというデータベースを作ります。
MariaDB [(none)]> CREATE DATABASE mydb;
Query OK, 1 row affected (0.002 sec)
Query OK, 1 row affected (0.002 sec)
USEでmydbを指定します。
MariaDB [(none)]> USE mydb;
Database changed
MariaDB [mydb]>
Database changed
MariaDB [mydb]>
CREATE TABLEでaisatsuというテーブルを作成します。
CREATE TABLE aisatsu (
id int(11) NOT NULL AUTO_INCREMENT,
text text NOT NULL,
PRIMARY KEY (id)
);
id int(11) NOT NULL AUTO_INCREMENT,
text text NOT NULL,
PRIMARY KEY (id)
);
実行します。
MariaDB [mydb]> CREATE TABLE aisatsu (
-> id int(11) NOT NULL AUTO_INCREMENT,
-> text text NOT NULL,
-> PRIMARY KEY (id)
-> );
Query OK, 0 rows affected (0.015 sec)
-> id int(11) NOT NULL AUTO_INCREMENT,
-> text text NOT NULL,
-> PRIMARY KEY (id)
-> );
Query OK, 0 rows affected (0.015 sec)
INSERTでレコードを追加します。4件追加してみました。
INSERT INTO aisatsu(text) VALUES ("Hello");
INSERT INTO aisatsu(text) VALUES ("Good morning");
INSERT INTO aisatsu(text) VALUES ("Good afternoon");
INSERT INTO aisatsu(text) VALUES ("Good evening");
INSERT INTO aisatsu(text) VALUES ("Good morning");
INSERT INTO aisatsu(text) VALUES ("Good afternoon");
INSERT INTO aisatsu(text) VALUES ("Good evening");
実行します。
MariaDB [mydb]> INSERT INTO aisatsu(text) VALUES ("Hello");
Query OK, 1 row affected (0.012 sec)
MariaDB [mydb]> INSERT INTO aisatsu(text) VALUES ("Good morning");
Query OK, 1 row affected (0.001 sec)
MariaDB [mydb]> INSERT INTO aisatsu(text) VALUES ("Good afternoon");
Query OK, 1 row affected (0.001 sec)
MariaDB [mydb]> INSERT INTO aisatsu(text) VALUES ("Good evening");
Query OK, 1 row affected (0.001 sec)
Query OK, 1 row affected (0.012 sec)
MariaDB [mydb]> INSERT INTO aisatsu(text) VALUES ("Good morning");
Query OK, 1 row affected (0.001 sec)
MariaDB [mydb]> INSERT INTO aisatsu(text) VALUES ("Good afternoon");
Query OK, 1 row affected (0.001 sec)
MariaDB [mydb]> INSERT INTO aisatsu(text) VALUES ("Good evening");
Query OK, 1 row affected (0.001 sec)
データベース情報の設定をします。database.phpはもとからあったものを書き換えました。最後が?>で閉じていないのが気になります。
database.php | ||
|
データベースの操作はコントローラではなくモデルに記述するようです。モデルはCI_Modelクラスを継承して実装します。今回はAisatsu_model.phpを作成しました。
Aisatsu_model.php | ||
|
コントローラはAisatu.phpという名前で作成しました。
Aisatsu.php | ||
|
受け取ったデータをforeachで表示するview.phpを作成しました。
view.php | ||
|
それぞれのphpファイルの配置は以下の通りです。
application/
+ config/
| + database.php
+ controllers/
| + Aisatsu.php
+ models/
| + Aisatsu_model.php
+ views/
+ aisatsu/
+ view.php
+ config/
| + database.php
+ controllers/
| + Aisatsu.php
+ models/
| + Aisatsu_model.php
+ views/
+ aisatsu/
+ view.php
以下のURLへアクセスしてみます。
http://localhost/CodeIgniter-develop/index.php/aisatsu/view
データベースのaisatsuテーブルに登録されているレコードが全て表示されました。
Copyright (C) 2022 ymlib.com