1:建立基於docker的mysql,參考
Mac上將brew安裝的MySql改用Docker執行
2:建立基於docker�php image
在當前目錄,建立Dockerfile,內容如下
FROM php:7.0-cli MAINTAINER Terry Zhang <zterry@qq.com> RUN docker-php-ext-install pdo_mysql mysqli
3.建立php鏡像
docker build -t php-mysql .
4. 編寫php腳本,可以從mysql數據庫讀取數據:
<?php $host = 'mysql'; $user = 'root'; $pwd = 'password'; $db = 'test'; $mysqli = new mysqli($host, $user, $pwd, $db); if ($mysqli->connect_errno) { echo "Errno: " . $mysqli->connect_errno . "\n"; } $sql = 'SELECT * FROM users'; if ($res = $mysqli->query($sql)) { while ($row = $res->fetch_assoc()) { print_r($row); } } ?>
5. 執行php的容器,參數如下: bash docker run -it --rm -v (pwd):/var --link my-mysql-server1:mysql php-mysql:latest php /var/mysql.php
需要注意的地方是--link參數,這里調用的是名為my-mysql-server1的容器,其在php容器中的host為mysql。可以通過如下命令進行驗證:
docker run -it --rm php-mysql ping mysql
如果一切順利,則會看到輸出結果;如果有問題,自行調試。
Then it's just a case of writing the PHP switch statement:
switch ($getDay) { case 'Monday': $comment = "Worst Day of the Week!"; break; case 'Tuesday': $comment = "1 Day Better Than Monday!"; break; case 'Wednesday': $comment = "Half Way There!"; break; case 'Thursday': $comment = "Getting There!"; break; case 'Friday': $comment = "Weekend At 5pm!"; break; case 'Saturday': $comment = "Relaxing Saturday!"; break; case 'Sunday': $comment = "Work Tomorrow!"; break; }
The $getDay is the that each case is checked against, and each case is one of the potential values of $getDay . So if it's 'Monday', then the variable will be 'Monday' and that'll match the first case value, so it'll set the variable to the string "Worst Day of the Week!".
Finally this can be output on the page using:
echo "Today is " . $getDay . " - " . $comment;
So this would echo out - Today is Monday - Worst Day of the Week! .