Add PHP/MySQL/PhpMyAdmin docker-compose
Signed-off-by: Alireza Far <imail01999@gmail.com>
This commit is contained in:
5
php-mysql-apache/php/Dockerfile
Normal file
5
php-mysql-apache/php/Dockerfile
Normal file
@@ -0,0 +1,5 @@
|
||||
FROM php:8.0-apache
|
||||
|
||||
COPY . /var/www/html
|
||||
|
||||
EXPOSE 80
|
26
php-mysql-apache/php/index.php
Normal file
26
php-mysql-apache/php/index.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* This is a sample code to show you how Docker containers work together.
|
||||
* DO NOT USE this source and pattern in production.
|
||||
*
|
||||
*
|
||||
* You can see the result by going to `localhost:8000`.
|
||||
* 8000-8080:
|
||||
* Host Apache port to run PHP index.php inside /var/www/html in the container.
|
||||
* 8001 : Refers to 3306 & 33060 of the MySQL port in the container
|
||||
* 8003 : Refers to 80 of the PhpMyAdmin [user=root, no password]
|
||||
*
|
||||
*/
|
||||
|
||||
require_once "query.php";
|
||||
|
||||
|
||||
/**
|
||||
* IP of MySQL
|
||||
* We can add specific IPs in docker-compose.yaml file for each service(container)
|
||||
*/
|
||||
$mysqlHost = "172.17.0.3";
|
||||
|
||||
run_MySQL_queries($mysqlHost);
|
73
php-mysql-apache/php/query.php
Normal file
73
php-mysql-apache/php/query.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
function run_MySQL_queries($mysqlHost)
|
||||
{
|
||||
/**
|
||||
* Connect to MySQL via PDO
|
||||
*
|
||||
* PDO modules must be installed before using, moduels' name:
|
||||
* pdo pdo_mysql
|
||||
*/
|
||||
$pdo = new PDO("mysql:host=$mysqlHost", "root", "");
|
||||
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Create a new database called `MyDatabase` if does not exist
|
||||
*/
|
||||
$pdo->exec("CREATE DATABASE IF NOT EXISTS MyDatabase");
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Use the database we created
|
||||
*/
|
||||
$pdo->exec("USE MyDatabase");
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Check if the `test` table exists before
|
||||
* If not, creates a new one
|
||||
*/
|
||||
$stmt = $pdo->prepare("SHOW TABLES LIKE 'test'");
|
||||
$stmt->execute();
|
||||
|
||||
if ($stmt->rowCount() == 0) {
|
||||
$stmt = $pdo->prepare("CREATE TABLE test (column1 VARCHAR (255) )");
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Insert a sample data `Hello world` into table
|
||||
*/
|
||||
$stmt = $pdo->prepare("INSERT INTO test (column1) VALUES ('Hello world')");
|
||||
$stmt->execute();
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Fetch the data we inserted
|
||||
*/
|
||||
$stmt = $pdo->prepare("SELECT * FROM test LIMIT 1");
|
||||
$stmt->execute();
|
||||
|
||||
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Show the data as string
|
||||
*/
|
||||
echo $result[0]['column1'];
|
||||
}
|
Reference in New Issue
Block a user