linkvz/link.php

75 lines
1.9 KiB
PHP
Raw Normal View History

2022-01-09 23:50:09 +01:00
<?php
class Link{
public $name;
public $url;
public function __construct($newName, $newUrl){
$this->name = $newName;
$this->url = $newUrl;
}
}
class dbLinkHandler
{
private $pdo;
public function __construct()
{
try
{
// Verbindung zur Datenbank herstellen
2022-01-11 13:48:53 +01:00
$this->pdo = new PDO('mysql:host=localhost;dbname=linkvz', 'linkvz', 'linkvz');
2022-01-09 23:50:09 +01:00
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Tabelle link erstellen, falls nicht vorhanden
$sql = 'CREATE TABLE IF NOT EXISTS link (
2022-01-12 21:03:02 +01:00
link_url VARCHAR(50) NOT NULL PRIMARY KEY,
2022-01-09 23:50:09 +01:00
link_name VARCHAR(100),
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)
{
echo $sql . "<br>" . $e->getMessage();
}
}
public function getListReverse()
{
// Mit verschachtelten Arrays optimieren!! Array schlüssel id enthält array mit schlüssel link_id, link_name, etc (repräsentiert datensatz)
$list = array();
2022-01-12 21:03:02 +01:00
$sql = "SELECT * FROM link ORDER BY link_name DESC";
2022-01-09 23:50:09 +01:00
return $this->pdo->query($sql);
}
public function getList()
{
// Mit verschachtelten Arrays optimieren!! Array schlüssel id enthält array mit schlüssel link_id, link_name, etc (repräsentiert datensatz)
$list = array();
$sql = "SELECT * FROM link";
return $this->pdo->query($sql);
}
}
?>