{"id":26,"date":"2025-01-08T16:05:56","date_gmt":"2025-01-08T15:05:56","guid":{"rendered":"http:\/\/cibis.de\/blog\/?p=26"},"modified":"2025-01-21T07:57:08","modified_gmt":"2025-01-21T06:57:08","slug":"linksammlung-teil-1-die-datenbank","status":"publish","type":"post","link":"https:\/\/cibis.de\/blog\/linksammlung-teil-1-die-datenbank\/2025\/","title":{"rendered":"Linksammlung Teil 1 &#8211; Die Datenbank"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Es geht los. Als erstes soll via PHP eine Verbindung zur mySQL Datenbank hergestellt werden. Au\u00dferdem soll dort eine Tabelle erstellt werden, in der alle relevanten Daten der Links abgespeichert werden.<\/p>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\/ Datenbank Zugangsdaten\n$servername = \"localhost\";\n$username = \"dein_benutzername\";\n$password = \"dein_passwort\";\n$dbname = \"db_name\";\n\n\/\/ Datenbankverbindung herstellen\n$conn = new mysqli($servername, $username, $password);\n\n\/\/ \u00dcberpr\u00fcfen, ob die Verbindung erfolgreich war\nif ($conn->connect_error) {\n    die(\"Verbindung fehlgeschlagen: \" . $conn->connect_error);\n}\n\n\/\/ Datenbank erstellen, falls sie noch nicht existiert\n$sql = \"CREATE DATABASE IF NOT EXISTS $dbname\";\nif ($conn->query($sql) === TRUE) {\n    \/\/echo \"Datenbank erfolgreich erstellt\";\n} else {\n    echo \"Fehler beim Erstellen der Datenbank: \" . $conn->error;\n}\n\n\/\/ Datenbank ausw\u00e4hlen\n$conn->select_db($dbname);\n\n\/\/ Tabelle erstellen, falls sie noch nicht existiert\n$sql = \"CREATE TABLE IF NOT EXISTS links (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    url VARCHAR(255) NOT NULL,\n    name VARCHAR(255) NOT NULL,\n    tags VARCHAR(255),\n    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n)\";\nif ($conn->query($sql) === TRUE) {\n    \/\/echo \"Tabelle erfolgreich erstellt\";\n} else {\n    echo \"Fehler beim Erstellen der Tabelle: \" . $conn->error;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Zuerst werden die Zugangsdaten eingegeben. Diese sind im Normalfall beim Hoster zu erfragen bzw. stehen im Kundenbereich.<br>Anschlie\u00dfend probiert sich das Script mit der Datenbank zu verbinden falls diese existiert; tut sie das nicht, wird sie erstellt. Genauso wird mit der Tabelle verfahren. Hier gibt es Spalten f\u00fcr die URL, den Titel, das Datum, wann der Link hinzugef\u00fcgt wurde und die Verschlagwortung.<br>Am Ende wird die Verbindung wieder beendet. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n<div class=\"gsp_post_data\" \r\n\t            data-post_type=\"post\" \r\n\t            data-cat=\"code,php\" \r\n\t            data-modified=\"120\"\r\n\t            data-created=\"1736352356\"\r\n\t            data-title=\"Linksammlung Teil 1 &#8211; Die Datenbank\" \r\n\t            data-home=\"https:\/\/cibis.de\/blog\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Es geht los. Als erstes soll via PHP eine Verbindung zur mySQL Datenbank hergestellt werden. Au\u00dferdem soll dort eine Tabelle erstellt werden, in der alle relevanten Daten der Links abgespeichert werden.<\/p>\n","protected":false},"author":1,"featured_media":29,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,7],"tags":[10,8,9],"class_list":["post-26","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code","category-php","tag-bookmark","tag-code","tag-mysql"],"_links":{"self":[{"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":7,"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":132,"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions\/132"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/media\/29"}],"wp:attachment":[{"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}