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を利用する。公式サイトから自分の環境に合わせてインストーラをダウンロードする。以下、動作実績のあるインストール手順である。
  1. インストーラのダウンロード
  2. 8.2.12/PHPのDownload(64 bit)をクリックし、インストーラをダウンロードする。インストーラを実行する際は、管理者権限で実行をすることを推奨する。 the capture of download site
  3. インストールの場所
  4. ダウンロードされたインストーラを起動すると次の警告がいきなりでてしまった。

    Warning about UAC

    この警告は、Windowsのユーザアカウント管理 (UAC: User Account Control)により、XAMPPのいくつかの機能が制限されてしまうかもしれないということを警告している。管理者権限でインストーラを実行するとこの警告が表示されなかったかもしれない。 今回は、インストール先のパスをC:\Program Files直下ではない場所にすることでこの警告を解決した。
  5. ApacheサーバとMySQLサーバの開始
  6. Control Panelを起動するかどうかを尋ねるポップアップが出現し、はいを選択すると次の画面が表示される。
    the capture of XAMPP Control Panel
    XAMPP Control Panelの初期画面
    上記のApache (Webサーバ), とMySQL(データベース)の右側にあるStartをクリックするとローカル環境でWebサーバとデータベースサーバが立ち上がり、画面が次のよう変化する。
    the capture of XAMMP Control Panel after running
    ApacheとMySQLのサーバの立ち上がり後
    デフォルトでApacheはTCP80 / TCP443, MySQLはTCP3306のポートを開く。サーバが立ち上がっているかを確認するために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からも指定ポートで通信するソケットが作成されているか確認することができる。
    Popup after clicking Netstat
    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つある。
  1. XAMMPのMySQLサーバのポート番号を変える。
  2. 3306のポート番号を使用しているプロセスを終了させる。

XAMPPのMySQLのポート番号を変える

  1. MySQLサーバの設定ファイル(my.ini)を開く。
  2. the capture of opening setting file (my.ini)
    Open a setting file (my.ini)
  3. port=3306の箇所を未使用の任意のポート番号に変えて保存する。ポート番号を指定する行は20行目と29行目の2つある。
  4. the content of a setting file (my.ini)
    MySQLサーバの設定ファイルの中身
  5. 再度MySQLサーバを開始する
  6. the capture of XAMMP Control Panel with other port number of MySQL
    TCP12345でMySQLサーバを準備
    上記の方法によりポート番号のコンフリクトを解決する。

3306のポート番号を使用しているプロセスを終了させる

  1. コマンドプロンプトからTCPポート3306を利用しているPIDを探す。次のコマンドで見つかる。
  2. > netstat -anof | findstr :3306

  3. タスクマネージャーを開き、左タブの詳細(Details)から先ほど調べたPIDのプロセスを探す。
  4. 該当するプロセスを選択した状態で右クリックをし、タスクを終了(End tasks)を実行する。
  5. the capture of task manager
    タスクマネージャーにおけるタスクの終了
  6. XAMPPコントロールパネルに戻り、MySQLサーバをスタートできる。

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.

the apperance when opening web server in browser

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.

appearance in a file explorer

Opening index.php, codes will be so:
<?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/