1: Build docker-based MySQL, reference
Mac will use brew-installed MySQL instead of Docker to perform
2: Build based on docker?php image
In the current directory, set up the Dockerfile, the content is as follows
From Php:7.0-cli maintainer Terry Zhang <[email protected]> RUN docker-php-ext-install pdo_mysql mysqli
3. Create a PHP image
Docker build-t Php-mysql.
4. Write PHP scripts that can read data from the MySQL database:
<?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. Execute the PHP container with the following parameters: Bash Docker run-it--rm-v (PWD):/var--link my-mysql-server1:mysql php-mysql:latest php/var/mysql.php
The point to note is the--link parameter, which is called a container named My-mysql-server1, whose host in the PHP container is MySQL. Can be verified by the following command:
Docker run-it--rm php-mysql Ping MySQL
If all goes well, you'll see the output, and if you have a problem, debug it yourself.
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 Tha n monday! "; Break Case ' Wednesday ': $comment = "half-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, which is checked against, and each case is one of the potential values of $getDay. So if it's ' Monday ', then the variable would 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 is output on the page using:
echo "Today is". $getDay. " - " . $comment;
So this would echo Out-today are Monday-worst day of the week!.
Docker-based PHP calls a Docker-based MySQL database method