Docker-based PHP calls a Docker-based MySQL database method

Source: Internet
Author: User
Tags php switch docker run

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.