Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created May 30, 2023 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/33cf669de1c37dbd6533c8f024346aee to your computer and use it in GitHub Desktop.
Save code-boxx/33cf669de1c37dbd6533c8f024346aee to your computer and use it in GitHub Desktop.
PHP Store & Retrieve HTML Code In MYSQL

PHP STORE & RETRIEVE HTML CODE IN MYSQL

https://code-boxx.com/store-retrieve-html-code-mysql-php/

NOTES

  1. Create a dummy database and import 1-contents-table.sql.
  2. Change the database settings in 2-lib-content.php to your own.
  3. Run 3a-insert-html.php and 3b-retrieve-html.php in the web browser.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

CREATE TABLE `web_contents` (
`id` bigint(20) NOT NULL,
`contents` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `web_contents`
ADD PRIMARY KEY (`id`);
ALTER TABLE `web_contents`
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT;
<?php
class Content {
// (A) CONSTRUCTOR - CONNECT TO DATABASE
private $pdo = null;
private $stmt = null;
public $lastID = null;
public $error = "";
function __construct() {
$this->pdo = new PDO(
"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET,
DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
}
// (B) DESTRUCTOR - CLOSE DATABASE CONNECTION
function __destruct() {
if ($this->stmt !== null) { $this->stmt = null; }
if ($this->pdo !== null) { $this->pdo = null; }
}
// (C) HELPER - EXECUTE SQL QUERY
function exec ($sql, $data=null) : void {
$this->stmt = $this->pdo->prepare($sql);
$this->stmt->execute($data);
}
// (D) SAVE CONTENT
function save ($html) {
$this->exec("INSERT INTO `web_contents` (`contents`) VALUES (?)", [$html]);
return true;
}
// (E) LOAD CONTENT
function load ($id) {
$this->exec("SELECT `contents` FROM `web_contents` WHERE `id`=?", [$id]);
return $this->stmt->fetchColumn();
}
}
// (F) DATABASE SETTINGS - CHANGE THESE TO YOUR OWN
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "root");
define("DB_PASSWORD", "");
// (G) CONTENT OBJECT
$_CONTENT = new Content();
<?php
// (A) LOAD CONTENT LIBRARY
require "2-lib-content.php";
// (B) INSERT HTML
echo $_CONTENT->save("<strong>HELLO WORLD</strong>")
? "OK" : "ERROR" ;
<?php
// (A) LOAD CONTENT LIBRARY
require "2-lib-content.php";
// (B) OUTPUT HTML ?>
<!DOCTYPE html>
<html>
<head>
<title>MYSQL HTML DEMO</title>
<meta charset="utf-8">
</head>
<body>
<?=$_CONTENT->load(1)?>
</body>
</html>
<?php
// (A) LOAD CONTENT LIBRARY
require "2-lib-content.php";
// (B) RETRIEVE HTML - CONVERT HTML ENTITIES
$html = htmlentities($_CONTENT->load(1));
// (C) OUTPUT HTML ?>
<!DOCTYPE html>
<html>
<head>
<title>MYSQL HTML DEMO</title>
<meta charset="utf-8">
</head>
<body>
<textarea><?=$html?></textarea>
<code><?=$html?></code>
</body>
</html>
<?php
// (A) LOAD CONTENT LIBRARY
require "2-lib-content.php";
// (B) HTML SNIPPET WITH SCRIPT
$html = "<p>Hi!</p> <script>alert('FOO')</script>";
// (C) ALLOW ONLY "P" TAGS
$html = strip_tags($html, ["p"]);
// (D) SAVE INTO DATABASE
echo $_CONTENT->save($html)
? "OK" : "ERROR" ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment