◆Xampp6.5.11、日本語とmySQL:メモ
Xampp6.5.11/PHP/mySQL設定のメモです。まずは実操作のみで解説は入れていません。

とにかく日本語!
Xampp6.5.11はmySQLなどの設定UIがどこにあるのか分かりづらくなっている上、日本語が化けます。
例えばmySQLの設定入口
http://localhost/security/index.php
画面などです。
PHP/mySQLなどもそのままでは日本語は取り扱えません。
で、まずは日本語対応。
設定するのは次のテキストファイルです。
- C:\xampp\security\htdocs\navi.php
- C:\xampp\security\htdocs\lang\jp.php
- C:\xampp\php\php.ini
- C:\xampp\mysql\bin\my.ini
インストールが成功してAphachまでは動いているとして、
次のファイル
C:\xampp\security\htdocs\navi.php
に1行追加します。
<td align="right" class="navi">
<a target=_parent class=n href="lang.php?de"><?php print $TEXT['navi-german']; ?></a><br>
<a target=_parent class=n href="lang.php?en"><?php print $TEXT['navi-english']; ?></a><br>
<a target=_parent class=n href="lang.php?es"><?php print $TEXT['navi-spanish']; ?></a><br>
<a target=_parent class=n href="lang.php?fr"><?php print $TEXT['navi-french']; ?></a><br>
<a target=_parent class=n href="lang.php?it"><?php print $TEXT['navi-italian']; ?></a><br>
<a target=_parent class=n href="lang.php?nl"><?php print $TEXT['navi-dutch']; ?></a><br>
<a target=_parent class=n href="lang.php?no"><?php print $TEXT['navi-norwegian']; ?></a><br>
<a target=_parent class=n href="lang.php?pl"><?php print $TEXT['navi-polish']; ?></a><br>
<a target=_parent class=n href="lang.php?pt"><?php print $TEXT['navi-portuguese']; ?></a><br>
<a target=_parent class=n href="lang.php?sl"><?php print $TEXT['navi-slovenian']; ?></a><br>
<a target=_parent class=n href="lang.php?zh"><?php print $TEXT['navi-chinese']; ?></a><p>
<a target=_parent class=n href="lang.php?jp"><?php print $TEXT['navi-japanese']; ?></a><p>
<p class="navi">©2002-<?php echo date("Y"); ?><br>
上のリストの赤い行が追加行です。日本語環境(jp)の設定になっています。
ファイルの文字コードの変更;jp.php
次のファイル
C:\xampp\security\htdocs\lang\jp.php
は本来UTF-8でなければならない所、shift-jisになっており、これをUTF-8に変更します。
例えばエディタ「秀丸」ではファイルを開いて、下のバーにある文字コード表示をクリックし ポップアップメニューからUnicode(UTF-8)を選択し、ダイアログで[内容を維持したまま変更]を 押し、「ファイル」メニュー⇒「上書き保存」で保存します。
phpシステムの文字コードの変更;php.ini
次のファイル
C:\xampp\php\php.ini
で文字コード設定をUTF-8にします。その他日本対応します。
なお、幾つかの項目はコメントアウトを外すだけです。
// 815行あたり ; PHP's default character set is set to empty. ; http://php.net/default-charset default_charset = "UTF-8" // 1069-1070あたり (ISO-8859-1⇒UTF-8) [iconv] iconv.input_encoding = UTF-8 iconv.internal_encoding = UTF-8 iconv.output_encoding = UTF-8 // 1872あたり (EUC-JP⇒UTF-8) ; internal/script encoding. ; Some encoding cannot work as internal encoding. ; (e.g. SJIS, BIG5, ISO-2022-*) ; http://php.net/mbstring.internal-encoding mbstring.internal_encoding = UTF-8 // 1046あたり (Europe/Berlin⇒Asia/Tokyo) [Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone date.timezone=Asia/Tokyo // 1866あたり [mbstring] ; language for internal character representation. ; http://php.net/mbstring.language mbstring.language = Japanese // 1876あたり ; http input encoding. ; http://php.net/mbstring.http-input mbstring.http_input = auto // 1881あたり (SJIS⇒pass) ; http output encoding. mb_output_handler must be ; registered as output buffer to function ; http://php.net/mbstring.http-output mbstring.http_output = pass // 1889あたり ; enable automatic encoding translation according to ; mbstring.internal_encoding setting. Input chars are ; converted to internal encoding by setting this to On. ; Note: Do _not_ use automatic encoding translation for ; portable libs/applications. ; http://php.net/mbstring.encoding-translation mbstring.encoding_translation = Off // 1894あたり ; automatic encoding detection order. ; auto means ; http://php.net/mbstring.detect-order mbstring.detect_order = auto // 1899あたり ; substitute_character used when character cannot be converted ; one from another ; http://php.net/mbstring.substitute-character mbstring.substitute_character = none;
mySQLの文字コードの変更;my.ini
次のファイル
C:\xampp\mysql\bin\my.ini
上の文字コード設定をUTF-8にします。
// 22行目あたり 追加 [client] # password = your_password port = 3306 socket = "C:/xampp/mysql/mysql.sock" default-character-set=utf8 // 43行目あたり 2行追加 [mysqld] port= 3306 socket = "C:/xampp/mysql/mysql.sock" basedir = "C:/xampp/mysql" tmpdir = "C:/xampp/tmp" datadir = "C:/xampp/mysql/data" pid_file = "mysql.pid" # enable-named-pipe key_buffer = 16M max_allowed_packet = 1M sort_buffer_size = 512K net_buffer_length = 8K read_buffer_size = 256K read_rnd_buffer_size = 512K myisam_sort_buffer_size = 8M log_error = "mysql_error.log" character-set-server=utf8 skip-character-set-client-handshake // 166 辺り 追加 [mysql] no-auto-rehash # Remove the next comment character if you are not familiar with SQL #safe-updates default-character-set=utf8
mySQLのrootパスワード設定(と日本語コード確認)
次のページhttp://localhost/security/xamppsecurity.php
を開きます。
パスワードを設定し、[パスワードを変更しました。]というヘンテコな表示のボタンを押します。
一旦、[XAMPP Control Panel]でmySQLを停止し、起動し直します。
コマンドプロンプトでmySQLにログインできる事と、文字コード設定がutf8になっている事を確認します。
C:\>cd \xampp\mysql\bin C:\xampp\mysql\bin> C:\xampp\mysql\bin>mysql -u root -p設定したパスワード Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.25 MySQL Community Server (GPL) Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show variables like 'char%'; +--------------------------+--------------------------------+ | Variable_name | Value | +--------------------------+--------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | C:\xampp\mysql\share\charsets\ | +--------------------------+--------------------------------+ 8 rows in set (0.00 sec) mysql> quit Bye C:\xampp\mysql\bin>
なお、-uの後ろには1個空白が必要で-pの後ろには空白を入れてはいけないという意味不明の 仕様となっていることに注意が必要です。
DBアクセスの基本確認とユーザ登録
コマンドプロンプトでmySQLが動作することを確認します。 さらに、作成したDBにアクセス可能なユーザを登録します。 次の例では、
データベース | test2 | |
テーブル | nameWithId |
ユーザ名 | tester1 | |
パスワード | 利用者パスワード |
C:\xampp\mysql\bin>mysql -u root -p設定したパスワード ... 省略 ... mysql> create database test2; Query OK, 1 row affected (0.00 sec) mysql> use test2; Database changed mysql> create table nameWithId (id int,name varchar(30)); Query OK, 0 rows affected (0.08 sec) mysql> insert into nameWithId values (1,'Romeo'); Query OK, 1 row affected (0.00 sec) mysql> insert into nameWithId values (2,'Juliet'); Query OK, 1 row affected (0.00 sec) mysql> select * from nameWithId; +------+--------+ | id | name | +------+--------+ | 1 | Romeo | | 2 | Juliet | +------+--------+ 2 rows in set (0.00 sec) mysql> grant all privileges on test2.* to tester1@localhost mysql> identified by '利用者パスワード'; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye C:\xampp\mysql\bin> C:\xampp\mysql\bin>mysql -u tester1 -p利用者パスワード Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.25 MySQL Community Server (GPL) Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | test | | test2 | +--------------------+ 3 rows in set (0.00 sec) mysql> use test2; Database changed mysql> select * from nameWithId; +------+--------+ | id | name | +------+--------+ | 1 | Romeo | | 2 | Juliet | +------+--------+ 2 rows in set (0.00 sec) mysql> quit Bye C:\xampp\mysql\bin>
PHPからアクセスできる事の確認
次のphpファイルを作成し
C:\xampp\htdocs\project\PHP_mySQL\mySQLtst1.php
にセットします。
<!DOCTYPE html> <html> <head> <title>PHP/mySQL試験</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> </head> <?php $db_con= mysql_connect("localhost","tester1","利用者パスワード"); if( $db_con==false ) die("DB接続失敗"); if( mysql_select_db("test2",$db_con)==false ) die("DB選択失敗"); $rows= mysql_query("select * from nameWithId",$db_con); if( !$rows ) die(mysql_error()); while( $row=mysql_fetch_array($rows) ){ echo $row["id"]; echo "."; echo $row["name"]; echo "<br>"; } ?> </body> </html>
ブラウザでアクセスすると次のような画面が得られます。

| 固定リンク