PHP
phpはサーバサイドで動くプログラミング言語である。データベースとの接続、SQLクエリの生成と送信、データベースの操作、クライアントのウェブブラウザに表示する情報(html, css, js)の動的生成などができる。本サイトでは、phpファイルのテスト環境の作成、SQLと通信を行うプログラムを紹介する。PHPのテスト環境を整える
XAMMPとは
XAMMP(ザンプ)はウェブアプリケーションをローカル環境でテストするためのツールであり、ウェブサーバ(Apache)、SQLサーバ(MySQL)などをローカル環境でテストすることができる。 PHPはウェブサーバ上で実行されるため、ウェブサーバである(Apache)を立てる必要がある。そのため、XAMMPをインストールする。 Windows OS上でXAMPPを利用して、phpコーディングのデバッグ作業をする流れを記述する。XAMPPは(ザンプ)と読む。XAMPPのインストール・実行
ローカル環境(自分のパソコン内部のみ)でphpのコーディングのテストを行うため、XAMPPを利用する。公式サイトから自分の環境に合わせてインストーラをダウンロードする。以下、動作実績のあるインストール手順である。- インストーラのダウンロード 8.2.12/PHPのDownload(64 bit)をクリックし、インストーラをダウンロードする。インストーラを実行する際は、管理者権限で実行をすることを推奨する。
- インストールの場所 ダウンロードされたインストーラを起動すると次の警告がいきなりでてしまった。
- ApacheサーバとMySQLサーバの開始 Control Panelを起動するかどうかを尋ねるポップアップが出現し、はいを選択すると次の画面が表示される。



netstat
を利用して確認することができる。
netstat -an | findstr /R "0.0.0.0:80 | 0.0.0.0:443 | 0.0.0.0:3306"
TCP 0.0.0.0:80 0.0.0.0:0 LISTENING
TCP 0.0.0.0:443 0.0.0.0:0 LISTENING
TCP 0.0.0.0:3306 0.0.0.0:0 LISTENING
次のようにXAMPPコントロールパネルのNetstatからも指定ポートで通信するソケットが作成されているか確認することができる。

サーバが立ち上がらない場合
ポート番号が別のプロセスによって占有されている場合次のようなエラーが出る。下記の例では、MySQLサーバのデフォルトポートTCP3306が別のプロセスで既に開かれている状態でXAMPPでMySQLのサーバを立ち上げようとして表示された。12:18:41 AM [Apache] Attempting to start Apache app...
12:18:42 AM [Apache] Status change detected: running
12:18:44 AM [mysql] Problem detected!
12:18:44 AM [mysql] Port 3306 in use by "Unable to open process"!
12:18:44 AM [mysql] MySQL WILL NOT start without the configured ports free!
12:18:44 AM [mysql] You need to uninstall/disable/reconfigure the blocking application
12:18:44 AM [mysql] or reconfigure MySQL and the Control Panel to listen on a different port
12:18:44 AM [mysql] Attempting to start MySQL app...
12:18:45 AM [mysql] Status change detected: running
12:18:46 AM [mysql] Status change detected: stopped
12:18:46 AM [mysql] Error: MySQL shutdown unexpectedly.
12:18:46 AM [mysql] This may be due to a blocked port, missing dependencies,
12:18:46 AM [mysql] improper privileges, a crash, or a shutdown by another method.
12:18:46 AM [mysql] Press the Logs button to view error logs and check
12:18:46 AM [mysql] the Windows Event Viewer for more clues
12:18:46 AM [mysql] If you need more help, copy and post this
12:18:46 AM [mysql] entire log window on the forums
上記の解決方法は2つある。
- XAMMPのMySQLサーバのポート番号を変える。
- 3306のポート番号を使用しているプロセスを終了させる。
XAMPPのMySQLのポート番号を変える
- MySQLサーバの設定ファイル(my.ini)を開く。
- port=3306の箇所を未使用の任意のポート番号に変えて保存する。ポート番号を指定する行は20行目と29行目の2つある。
- 再度MySQLサーバを開始する



3306のポート番号を使用しているプロセスを終了させる
- コマンドプロンプトからTCPポート3306を利用しているPIDを探す。次のコマンドで見つかる。
- タスクマネージャーを開き、左タブの詳細(Details)から先ほど調べたPIDのプロセスを探す。
- 該当するプロセスを選択した状態で右クリックをし、タスクを終了(End tasks)を実行する。
- XAMPPコントロールパネルに戻り、MySQLサーバをスタートできる。
> netstat -anof | findstr :3306

PHPのテスト
Open in Browser
You can open the website by entering any of following links on your usual web browser like Google Chrome and Microsoft Edge.127.0.0.1
127.0.0.1:80
localhost
localhost:80
http://127.0.0.1
http://127.0.0.1:80
http://localhost
http://localhost:80
Once you see the following screen, it has been successful.
Add Your Own File
The content shown in the first place is in a form of .html files. Specify the path where xampp was installed, and go to /xampp/htdocs. In this folder, index.php is the very first file transmitted from a web-server to client-browser.<?php
if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) {
$uri = 'https://';
} else {
$uri = 'http://';
}
$uri .= $_SERVER['HTTP_HOST'];
header('Location: '.$uri.'/dashboard/');
exit;
?>
Something is wrong with the XAMPP installation :-(
phpMyAdminにアクセスする
http://localhost/phpmyadmin/