-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.php
58 lines (51 loc) · 2.01 KB
/
db.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
<?php
class DB{
public function __construct($host, $db, $user, $password, $table_name="parser_data")
{
$this->conn = mysqli_connect($host, $user, $password, $db);
$this->tablename = $table_name;
$this->createTable();
}
function __destruct()
{
mysqli_close($this->conn);
}
public function query($query)
{
if(mysqli_query($this->conn, $query)){
return true;
}
echo "\n";
echo $query . " : ";
echo mysqli_error($this->conn);
echo "\n";
return false;
}
public function dropTable(){
$query = 'DROP TABLE IF EXISTS `' . $this->tablename . '`';
$this->query($query);
}
public function createTable()
{
$query = "CREATE TABLE IF NOT EXISTS `{$this->tablename}` " .
"(`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, `model_id` int(11) NOT NULL, `review_id` int(11) NOT NULL UNIQUE, " .
"`review_good` text, `review_bad` text, `review_comment` text, `review_grade` int(11), " .
"`review_author` varchar(255), `review_date` date) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
$this->query($query);
}
private function createValues($reviewObj){
return "SELECT {$reviewObj['model_id']}, {$reviewObj['review_id']}, '{$reviewObj['review_good']}', '{$reviewObj['review_bad']}', '{$reviewObj['review_comment']}', {$reviewObj['review_grade']}, '{$reviewObj['review_author']}', '{$reviewObj['review_date']}'";
}
public function addReview($reviewObj)
{
$query = "INSERT INTO `{$this->tablename}` (`model_id`, `review_id`, `review_good`, `review_bad`, `review_comment`, `review_grade`, `review_author`, `review_date`) {$this->createValues($reviewObj)} WHERE NOT EXISTS (SELECT review_id FROM {$this->tablename} WHERE review_id = {$reviewObj['review_id']}) LIMIT 1";
$this->query($query);
}
public function addReviews($reviews)
{
foreach($reviews as $review) {
$this->addReview($review);
echo "review added\n";
}
}
}