linkvz/link_lib.php

95 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2022-01-13 00:22:32 +01:00
<?php
class Link{
public $name;
public $url;
public $reg_date;
public function __construct($newName, $newUrl)
{
$this->name = $newName;
$this->url = $newUrl;
}
}
class dbLinkHandler
{
private $pdo;
public function __construct()
{
try
{
// Verbindung zur Datenbank herstellen
$this->pdo = new PDO('mysql:host=localhost;dbname=linkvz', 'linkvz', 'linkvz');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Tabelle link erstellen, falls nicht vorhanden
$sql = 'CREATE TABLE IF NOT EXISTS link (
link_url VARCHAR(100) NOT NULL PRIMARY KEY,
link_name VARCHAR(50),
reg_date TIMESTAMP
)';
$this->pdo->exec($sql); // use exec() because no results are returned
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
}
public function saveLink($link)
{
try
{
$statement = $this->pdo->prepare('INSERT INTO link(link_url, link_name) VALUES (:link_url, :link_name)');
$statement->execute
([
':link_url' => $link->url,
':link_name' => $link->name
]);
}
catch(PDOException $e)
{
if ($e->errorInfo[1] == 1062)
{
echo "haaaaaa!<br>";
}
else
{
echo $e->getMessage();
}
}
}
public function getListReverse()
{
$statement = $this->pdo->prepare("SELECT * FROM link ORDER BY reg_date DESC");
$statement->execute();
return $statement;
}
public function getList()
{
$statement = $this->pdo->prepare("SELECT * FROM link");
$statement->execute();
return $statement;
}
public function getLink($url)
{
$statement = $this->pdo->prepare("SELECT * FROM link WHERE link_url = ?");
$statement->execute(array($url));
$fetch = $statement->fetch();
$link = new link($fetch[1], $fetch[0]);
$link->reg_date = $fetch[2];
return $link;
}
}
?>