From 6bf1b9669dcbe37af672368822b25988cca050e9 Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Fri, 7 Feb 2025 17:00:20 -0700 Subject: [PATCH 1/3] database connection works on localhost, now testing on server. --- db-start/composer.json | 17 +++++++++++++++++ db-start/css/.gitkeep | 0 db-start/css/styles.css | 0 db-start/img/.gitkeep | 0 db-start/includes/.gitkeep | 0 db-start/index.php | 34 ++++++++++++++++++++++++++++++++++ db-start/js/.gitkeep | 0 db-start/js/index.js | 0 db-start/pal.sql | 15 +++++++++++++++ 9 files changed, 66 insertions(+) create mode 100644 db-start/composer.json create mode 100644 db-start/css/.gitkeep create mode 100644 db-start/css/styles.css create mode 100644 db-start/img/.gitkeep create mode 100644 db-start/includes/.gitkeep create mode 100644 db-start/index.php create mode 100644 db-start/js/.gitkeep create mode 100644 db-start/js/index.js create mode 100644 db-start/pal.sql diff --git a/db-start/composer.json b/db-start/composer.json new file mode 100644 index 0000000..8224d85 --- /dev/null +++ b/db-start/composer.json @@ -0,0 +1,17 @@ +{ + "name": "violet/db-start", + "autoload": { + "psr-4": { + "Violet\\DbStart\\": "src/" + } + }, + "authors": [ + { + "name": "Joshua Ashton", + "email": "joshuatashton+dev@gmail.com" + } + ], + "require": { + "vlucas/phpdotenv": "^5.6" + } +} diff --git a/db-start/css/.gitkeep b/db-start/css/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/db-start/css/styles.css b/db-start/css/styles.css new file mode 100644 index 0000000..e69de29 diff --git a/db-start/img/.gitkeep b/db-start/img/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/db-start/includes/.gitkeep b/db-start/includes/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/db-start/index.php b/db-start/index.php new file mode 100644 index 0000000..91bd9a2 --- /dev/null +++ b/db-start/index.php @@ -0,0 +1,34 @@ +safeLoad(); +$db_pass = $_ENV['DATABASE_PASSWORD']; + +// Make some constants +if ($_SERVER['HTTP_HOST'] == 'localhost') { + define('HOST', 'localhost'); + define('USER', 'root'); + define('PASS', $db_pass); + define('DB', 'palindromes'); +} else { + define('HOST', 'cs2440.joshashton.dev'); + define('USER', 'root'); + define('PASS', $db_pass); + define('DB', 'palindromes'); +} + +// Connect to the DB +$conn = mysqli_connect(HOST, USER, PASS, DB); + +// Write a DB query +$sql = 'SELECT * FROM palindrome;'; + +// Run DB query +$results = mysqli_query($conn, $sql); + +// Loop through data +while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) { + echo $row['phrase'] . '
'; +}; +?> diff --git a/db-start/js/.gitkeep b/db-start/js/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/db-start/js/index.js b/db-start/js/index.js new file mode 100644 index 0000000..e69de29 diff --git a/db-start/pal.sql b/db-start/pal.sql new file mode 100644 index 0000000..b66479c --- /dev/null +++ b/db-start/pal.sql @@ -0,0 +1,15 @@ +-------------------------- copy and paste to your local mysql server ------------------------------- +drop database if exists palindromes; +create database palindromes; +use palindromes; +create table palindrome (id int auto_increment primary key, phrase varchar(255)); +insert into palindrome (phrase) values ('race car'), ('bob'), ('senile felines'), ('stack cats'); +select * from palindrome; +---------------------------------------------------------------------------------------------------- + + +---------------------------- copy and paste to your remote server ---------------------------------- +create table palindrome (id int auto_increment primary key, phrase varchar(255)); +insert into palindrome (phrase) values ('race car'), ('bob'), ('senile felines'), ('stack cats'); +select * from palindrome; +---------------------------------------------------------------------------------------------------- From b8052613efb494ec2faa906231169a5d58ac9178 Mon Sep 17 00:00:00 2001 From: Josh Ashton Date: Sat, 8 Feb 2025 00:54:05 +0000 Subject: [PATCH 2/3] localhost and server configuration are the same. --- db-start/index.php | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/db-start/index.php b/db-start/index.php index 91bd9a2..66710a2 100644 --- a/db-start/index.php +++ b/db-start/index.php @@ -6,17 +6,10 @@ $dotenv->safeLoad(); $db_pass = $_ENV['DATABASE_PASSWORD']; // Make some constants -if ($_SERVER['HTTP_HOST'] == 'localhost') { - define('HOST', 'localhost'); - define('USER', 'root'); - define('PASS', $db_pass); - define('DB', 'palindromes'); -} else { - define('HOST', 'cs2440.joshashton.dev'); - define('USER', 'root'); - define('PASS', $db_pass); - define('DB', 'palindromes'); -} +define('HOST', '173.255.248.228'); +define('USER', 'root'); +define('PASS', $db_pass); +define('DB', 'palindromes'); // Connect to the DB $conn = mysqli_connect(HOST, USER, PASS, DB); From 5a3dc5a618e2eed44c2c52a9bfd22713a143dbee Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Fri, 7 Feb 2025 18:06:34 -0700 Subject: [PATCH 3/3] moved ip address to environment variable. --- {db-start => db-test}/composer.json | 0 {db-start => db-test}/css/.gitkeep | 0 {db-start => db-test}/css/styles.css | 0 {db-start => db-test}/img/.gitkeep | 0 {db-start => db-test}/includes/.gitkeep | 0 {db-start => db-test}/index.php | 16 ++++++++++++---- {db-start => db-test}/js/.gitkeep | 0 {db-start => db-test}/js/index.js | 0 {db-start => db-test}/pal.sql | 0 9 files changed, 12 insertions(+), 4 deletions(-) rename {db-start => db-test}/composer.json (100%) rename {db-start => db-test}/css/.gitkeep (100%) rename {db-start => db-test}/css/styles.css (100%) rename {db-start => db-test}/img/.gitkeep (100%) rename {db-start => db-test}/includes/.gitkeep (100%) rename {db-start => db-test}/index.php (59%) rename {db-start => db-test}/js/.gitkeep (100%) rename {db-start => db-test}/js/index.js (100%) rename {db-start => db-test}/pal.sql (100%) diff --git a/db-start/composer.json b/db-test/composer.json similarity index 100% rename from db-start/composer.json rename to db-test/composer.json diff --git a/db-start/css/.gitkeep b/db-test/css/.gitkeep similarity index 100% rename from db-start/css/.gitkeep rename to db-test/css/.gitkeep diff --git a/db-start/css/styles.css b/db-test/css/styles.css similarity index 100% rename from db-start/css/styles.css rename to db-test/css/styles.css diff --git a/db-start/img/.gitkeep b/db-test/img/.gitkeep similarity index 100% rename from db-start/img/.gitkeep rename to db-test/img/.gitkeep diff --git a/db-start/includes/.gitkeep b/db-test/includes/.gitkeep similarity index 100% rename from db-start/includes/.gitkeep rename to db-test/includes/.gitkeep diff --git a/db-start/index.php b/db-test/index.php similarity index 59% rename from db-start/index.php rename to db-test/index.php index 66710a2..3b51870 100644 --- a/db-start/index.php +++ b/db-test/index.php @@ -4,12 +4,20 @@ require_once 'vendor/autoload.php'; $dotenv = Dotenv\Dotenv::createImmutable(__DIR__); $dotenv->safeLoad(); $db_pass = $_ENV['DATABASE_PASSWORD']; +$ip_addr = $_ENV['IP_ADDRESS']; // Make some constants -define('HOST', '173.255.248.228'); -define('USER', 'root'); -define('PASS', $db_pass); -define('DB', 'palindromes'); +if ($_SERVER['HTTP_HOST'] == 'localhost') { + define('HOST', 'localhost'); + define('USER', 'root'); + define('PASS', $db_pass); + define('DB', 'palindromes'); +} else { + define('HOST', $ip_addr); + define('USER', 'root'); + define('PASS', $db_pass); + define('DB', 'palindromes'); +} // Connect to the DB $conn = mysqli_connect(HOST, USER, PASS, DB); diff --git a/db-start/js/.gitkeep b/db-test/js/.gitkeep similarity index 100% rename from db-start/js/.gitkeep rename to db-test/js/.gitkeep diff --git a/db-start/js/index.js b/db-test/js/index.js similarity index 100% rename from db-start/js/index.js rename to db-test/js/index.js diff --git a/db-start/pal.sql b/db-test/pal.sql similarity index 100% rename from db-start/pal.sql rename to db-test/pal.sql