seperate class for dbconnection

created class for Movie reindexing
This commit is contained in:
lukas-heiligenbrunner 2020-04-04 12:21:30 +02:00
parent ddf6140c32
commit 5448d85c18
5 changed files with 70 additions and 25 deletions

View File

@ -19,6 +19,7 @@
<div class="mediawrapper"> <div class="mediawrapper">
<div class="mediatile">vid1</div> <div class="mediatile">vid1</div>
<div class="mediatile">vid2</div> <div class="mediatile">vid2</div>
<img id="tempici" alt="mylabel">
</div> </div>
</body> </body>

View File

@ -1,5 +1,5 @@
Req.ready((event) => { Req.ready(() => {
Req.post("php/movie.php", "action=getMovies&test=2", function (dta) { Req.post("php/index.php", "action=getMovies&test=2", function (dta) {
console.log(dta); console.log(dta);
for (const ii in dta.data) { for (const ii in dta.data) {
document.getElementsByClassName("mediawrapper").item(0).insertAdjacentHTML('beforeend', ` document.getElementsByClassName("mediawrapper").item(0).insertAdjacentHTML('beforeend', `
@ -7,15 +7,16 @@ Req.ready((event) => {
} }
}); });
Req.post("php/movie.php", "action=storePic", function (dta) { Req.post("php/index.php", "action=storePic", function (dta) {
console.log(dta); console.log(dta);
}); });
Req.post("php/movie.php", "action=readPic", function (dta) { Req.post("php/index.php", "action=readPic", function (dta) {
console.log(dta); console.log(dta);
document.getElementById("tempici").src = dta.data;
}); });
Req.post("php/movie.php", "action=getDbSize", function (dta) { Req.post("php/index.php", "action=getDbSize", function (dta) {
console.log(dta); console.log(dta);
}); });
}); });

34
php/Database.php Normal file
View File

@ -0,0 +1,34 @@
<?php
class Database
{
private static $instance = null;
private $conn;
private $servername = "192.168.0.177";
private $username = "myuser";
private $password = "1qayxsw2";
private $dbname = "test";
// The db connection is established in the private constructor.
private function __construct()
{
// Create connection
$this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
}
public static function getInstance()
{
if (!self::$instance) {
self::$instance = new Database();
}
return self::$instance;
}
public function getConnection()
{
return $this->conn;
}
}

10
php/Movie.php Normal file
View File

@ -0,0 +1,10 @@
<?php
class Movie
{
public function reindexMovies()
{
}
}

View File

@ -1,18 +1,7 @@
<?php <?php
$servername = "192.168.0.177"; require 'Database.php';
$username = "myuser";
$password = "1qayxsw2";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
echo('{"data":"error"}');
return;
}
$conn = Database::getInstance()->getConnection();
if (isset($_POST['action'])) { if (isset($_POST['action'])) {
$action = $_POST['action']; $action = $_POST['action'];
@ -21,7 +10,7 @@ if (isset($_POST['action'])) {
echo('{"data":[{"url":"./vid.mp4"}]}'); echo('{"data":[{"url":"./vid.mp4"}]}');
break; break;
case "getDbSize": case "getDbSize":
$query = "SELECT table_schema AS \"Database\", $query = "SELECT table_schema AS \"Database\",
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS \"Size\" ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS \"Size\"
FROM information_schema.TABLES FROM information_schema.TABLES
WHERE TABLE_SCHEMA='test' WHERE TABLE_SCHEMA='test'
@ -31,26 +20,36 @@ if (isset($_POST['action'])) {
if ($result->num_rows == 1) { if ($result->num_rows == 1) {
$row = $result->fetch_assoc(); $row = $result->fetch_assoc();
echo '{"data":"'.$row["Size"].'"}'; echo '{"data":"' . $row["Size"] . '"}';
} }
break; break;
case "storePic": case "storePic":
// Select file type // Select file type
$imageFileType = strtolower(pathinfo("./lukas.png",PATHINFO_EXTENSION)); $imageFileType = strtolower(pathinfo("./lukas.png", PATHINFO_EXTENSION));
// Convert to base64 // Convert to base64
$image_base64 = base64_encode(file_get_contents("./lukas.png") ); $image_base64 = base64_encode(file_get_contents("./lukas.png"));
$image = 'data:image/'.$imageFileType.';base64,'.$image_base64; $image = 'data:image/' . $imageFileType . ';base64,' . $image_base64;
// Insert record // Insert record
$query = "insert into temppic(data) values('".$image."')"; $query = "insert into temppic(data) values('" . $image . "')";
if ($conn->query($query) === TRUE) { if ($conn->query($query) === TRUE) {
echo('{"data":"successfully created entry"}'); echo('{"data":"successfully created entry"}');
} else { } else {
echo('{"data":"'.$conn->error.'"}'); echo('{"data":"' . $conn->error . '"}');
} }
break;
case "readPic":
$query = "SELECT * FROM temppic WHERE id='5'";
$result = $conn->query($query);
$row = $result->fetch_assoc();
echo('{"data":"' . $row["data"] . '"}');
break; break;
} }
} else { } else {
echo('{"data":"error"}'); echo('{"data":"error"}');
} }
$conn->close(); $conn->close();
return;