少しづつWebサービス開発を続けています。DBにデータを保存し、PHPから呼び出すみたいなことをする必要があり、それも全てCloud9上に出来てしまうというのでやってみます。DBはレンタルサーバが対応していて、よく使われているものをチョイスすれば間違いないだろうと、MySQLを選択しました。
DBを作成するため、Cloud9のTerminal起動して以下コマンドを入力します。
$ mysql-ctl start
$ mysql-ctl cli
するとMySQLを使うぞモードになるので、testというDBを作成。
mysql> create database test;
本当に作られたか確認します。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| c9 |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.02 sec)
testというDBが作られてます。なんか他にも表示されてますが、デフォで存在するみたいですね。このあとDBにTable作ったり、実際の値入れたりあるんですが、そこはまた後日。
WorkspaceのPHPファイルを開いて、Cloud9の公式ヘルプからもってきた以下をコピペします。
// A simple PHP script demonstrating how to connect to MySQL.
// Press the 'Run' button on the top to start the web server,
// then click the URL that is emitted to the Output tab of the console.
$servername = getenv('IP');
$username = getenv('C9_USER');
$password = "";
$database = "test";
$dbport = 3306;
// Create connection
$db = new mysqli($servername, $username, $password, $database, $dbport);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
echo "Connected successfully (".$db->host_info.")";
プレビューすると、
Connected successfully
って出てるから、きっと大丈夫!