database connection works on localhost, now testing on server.
This commit is contained in:
@@ -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"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
require_once 'vendor/autoload.php';
|
||||
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
|
||||
$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');
|
||||
}
|
||||
|
||||
// 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'] . '<br>';
|
||||
};
|
||||
?>
|
||||
@@ -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;
|
||||
----------------------------------------------------------------------------------------------------
|
||||
Reference in New Issue
Block a user