· 9 years ago · Jul 29, 2017, 08:02 AM
1<?php
2 class mysqliControl {
3 private $dbhost;
4 private $dbuser;
5 private $dbpass;
6 private $dbname;
7
8 private $connected;
9 private $errorfeedb
10
11 function __construct( $host, $user, $pass, $name, $errorfeedback=false ) {
12 $this->dbhost = $host;
13 $this->dbuser = $user;
14 $this->dbpass = $pass;
15 $this->dbname = $name;
16
17 $this->dblink = new MySQLi( $this->dbhost, $this->dbuser,
18 $this->dbpass, $this->dbname );
19
20 $this->connected = true;
21 $this->errorfeedback = $errorfeedback;
22 }
23
24 function query( $query ) {
25 $qresult = mysqli_query( $this->dblink, $query );
26
27 if( $this->errorfeedback ) {
28 if( $qresult != false ) {
29 return $qresult;
30 } else {
31 print "ERROR IN '" . $query . "'<br/>";
32 }
33 } else {
34 return $qresult;
35 }
36 }
37
38 function cleaninput( $input ) {
39 $out = str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $input);
40
41 $removalwords = array("/delete/i", "/update/i","/union/i","/insert/i","/drop/i","/http/i","/--/i");
42 $out = preg_replace($removalwords, "", $out);
43
44 return $out;
45 }
46
47 function maketable( $tablename, $values) {
48 $tablequery = "CREATE TABLE IF NOT EXISTS ";
49
50 $tablequery = $tablequery . $this->cleaninput( $tablename ) . " (" . $values . ")";
51
52 $this->query( $tablequery );
53 }
54
55 function updatevalue( $tablename, $column1, $value1, $column2, $create=true ) {
56 $querystring = "SELECT * FROM " . $this->cleaninput( $tablename ) . " WHERE " . $this->cleaninput( $column1 ) . " = '" . $this->cleaninput( $value1 ) . "'";
57
58 $result = $this->query( $querystring );
59
60 if( $result->num_rows ) {
61 $querystring = "UPDATE " . $this->cleaninput( $tablename ) . " SET " . $this->cleaninput( $column2 ) . " = " . $this->cleaninput( $column2 ) . " + 1 WHERE " . $this->cleaninput( $column1 ) . " = '" . $this->cleaninput( $value1 ) . "'";
62
63 $this->query( $querystring );
64 } else {
65 $querystring = "INSERT INTO " . $this->cleaninput( $tablename ) . " (" . $this->cleaninput( $column1 ) . ", " . $this->cleaninput( $column2 ) . ") VALUES ('" . $this->cleaninput( $value1 ) . "', '1')";
66
67 $this->query( $querystring );
68 }
69 }
70
71 function select( $table, $column ) {
72 $selectquery = "SELECT " . $column . " FROM " . $table;
73
74 $result = $this->query( $selectquery );
75
76 $numrows = $result->num_rows;
77
78 $content = array();
79
80 for( $i = 1; $i <= $numrows; $i++ ) {
81 $currentline = $result->fetch_row();
82
83 $content[$i] = $currentline;
84 }
85
86 return $content;
87 }
88
89 }
90?>