This repository has been archived by the owner on Dec 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathDatabase.php
executable file
·93 lines (66 loc) · 2.51 KB
/
Database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/**
* Copyright (c) 2015 Leonardo Cardoso (http://leocardz.com)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* Version: 1.0.0
*/
/** This class is for database connection. It's just an example, neither security is being handled here nor mysql errors that might be occurred. */
include_once "Highlight.php";
class Database
{
static function insert($save)
{
$conn = Database::connect();
$save->text = mysqli_real_escape_string($conn, $save->text);
$save->title = mysqli_real_escape_string($conn, $save->title);
$save->description = mysqli_real_escape_string($conn, $save->description);
$query =
"INSERT INTO
`linkpreview`.`linkpreview`
(`id`, `text`, `image`, `title`, `canonicalUrl`, `url`, `pageUrl`, `description`, `videoIframe`)
VALUES (NULL, '" . $save->text . "', '" . $save->image . "', '" . $save->title . "', '" . $save->canonicalUrl . "',
'" . $save->url . "', '" . $save->pageUrl . "', '" . $save->description . "', '" . $save->videoIframe . "')";
mysqli_query($conn, $query);
$id = mysqli_insert_id($conn);
Database::close($conn);
return $id;
}
static function delete($delete)
{
$conn = Database::connect();
$query = "DELETE FROM `linkpreview`.`linkpreview` WHERE `id` = '" . $delete["id"] . "'";
mysqli_query($conn, $query);
Database::close($conn);
}
static function connect()
{
$host = "localhost";
$user = "root";
$password = "";
$database = "linkpreview";
if (!($connection = mysqli_connect($host, $user, $password, $database))) ;
mysqli_query($connection, "SET character_set_results=utf8");
mb_language('uni');
mb_internal_encoding('UTF-8');
mysqli_query($connection, "set names 'utf8'");
return $connection;
}
static function close($conn)
{
mysqli_close($conn);
}
static function select()
{
$conn = Database::connect();
$sth = mysqli_query($conn, "SELECT * FROM `linkpreview`.`linkpreview` ORDER BY id DESC");
$rows = array();
while ($r = mysqli_fetch_assoc($sth)) {
$r["text"] = Highlight::url($r["text"]);
$r["description"] = Highlight::url($r["description"]);
$rows[] = $r;
}
return $rows;
}
}