Type whatever to find the question. ex: (How to ... ?, چۆن ... ؟)
Type whatever to find the question. ex: (How to ... ?, چۆن ... ؟)
Reported
Questions
Help Center
Support Center

چۆن بتوانم mysqli بەکاربهێنم لەگەڵ OOP ؟

386 2 Answer
چۆن بتوانم mysqli بەکاربهێنم لەگەڵ OOP ؟ سڵاو، پێویستیم بە کڵاسێکی (OOP)یە بۆ ئەوەی بتوانم داتا insert بکەم و پاشان بانگیان بکەمەوە.
OOPPHPMysqli

Answers

Nashwan Answered 1 month ago
ئەتوانیت بەم شێوازەش لە ڕێگای ئۆ ئۆپیەوە داتا داخل بکەیت:
Php
            
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?PHP class Connect{ public $db; public function Connection(){ $this->db= new mysqli("HostName","user","password","DatabaseName"); if($this->db->connect_error){ die("connection failed"); } public function Query($queries){ $result = $this->db->Query($queries); if(!$queries){ die("query failed"); } } } } ?>
Replies
1
Mohammed D Mirzada Answered 3 month ago
سڵاو، دەتوانیت ئەم کڵاسە بەکاربهێنیت بۆ کۆنێکت کردنی وێبسایتەکەت لەگەڵ داتابەیس، وە بەم جۆرە لەگەڵ داتا مامەڵە دەکات: - INSERT - UPDATE - DELETE - SELECT کاتێک داتایەک داخڵ دەکەیت خۆکارانە دواهەمین IDی داخڵکراوەکەت بۆ دەگەڕێنێتەوە، وە کڵاسەکە ئێڕۆر لۆگیش لەخۆدەگرێت بۆ ئەوەی تووشی هیچ کێشەیەک نەبیت و چارەسەرەکەی بە ئاسانی بدۆزیتەوە. وە هەروەها ژماردنی هەموو ئەو داتایانەی کە داخڵی تەیبڵێک کراوە. وە بوونی فلتەر و فەنکشنێکی تایبەت کە ڕێگری دەکات لە هێرشی XSS و SQL Injection.
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
<?php /** * Programnas.com * db.php * 2021-04-27 05:02:08 * Created by: Mohammed D Mirzada */ class db{ const SERVER_NAME = "localhost"; const USERNAME = "root"; const PASSWORD = ""; const DATABASE_NAME = "db_test"; public $_connect, $_inserted_last_id, $_results; public $_mysqli_logs = array(); //CONNECTING TO THE DATABASE public function __construct(){ $mysqli = new mysqli( self::SERVER_NAME, self::USERNAME, self::PASSWORD, self::DATABASE_NAME ); //DETECT IF THERE WAS A FAILURE CONNECTION if ($mysqli->connect_error) { die("Connection failed: " . $mysqli->connect_error); }else{ $this->_connect = $mysqli; array_push($this->_mysqli_logs, "(" . date("Y-m-d H:i:s") . ") Database Connected Successfully"); } } //INSERT DATA INTO DATABASE public function Insert($table_name,$array){ $row_names = "("; $column_values = "VALUES ("; $i = 0; $len = count($array); foreach ($array as $key => $value) { $row_names .= $this->PreventXSSandSQLinjection($key); $column_values .= "'".$this->PreventXSSandSQLinjection($value)."'"; if ($len != 1 && $i != $len - 1) { $row_names .= ", "; $column_values .= ", "; } $i++; } $row_names .= ") "; $column_values .= ")"; //QUERY $sql = "INSERT INTO ".$table_name." ".$row_names.$column_values.""; if ($this->_connect->query($sql) === TRUE) { //log array_push($this->_mysqli_logs, "(" . date("Y-m-d H:i:s") . ") Inserted Successfully"); //inserted last id $this->_inserted_last_id = $this->_connect->insert_id; //true means inserted return true; }else{ //log array_push($this->_mysqli_logs, "(" . date("Y-m-d H:i:s") . ") Error: " . $this->_connect->error); //false means there is an insertion error return false; } //RETURN BOOLEAN } //UPDATE DATABASE TABLE public function Update($table_name,$array,$id){ $data = ""; $i = 0; $len = count($array); foreach ($array as $key => $value) { $data .= $this->PreventXSSandSQLinjection($key)."="."'".$this->PreventXSSandSQLinjection($value)."'".""; if ($len != 1 && $i != $len - 1) { $data .= ", "; } $i++; } //QUERY $sql = "UPDATE ".$table_name." SET ".$data." WHERE id = ".$id.""; if ($this->_connect->query($sql) === TRUE) { //log array_push($this->_mysqli_logs, "(" . date("Y-m-d H:i:s") . ") Updated Successfully"); //true means updated return true; }else{ //log array_push($this->_mysqli_logs, "(" . date("Y-m-d H:i:s") . ") Error: " . $this->_connect->error); //false means there is an updating error return false; } //RETURN BOOLEAN } //DELETE DATA FROM DATABASE TABLE public function Delete($table,$id){ //QUERY $sql = "DELETE FROM ".$table." WHERE id=".$id.""; if ($this->_connect->query($sql) === TRUE) { //log array_push($this->_mysqli_logs, "(" . date("Y-m-d H:i:s") . ") Deleted Successfully"); //true means deleted return true; } else { //log array_push($this->_mysqli_logs, "(" . date("Y-m-d H:i:s") . ") Error: " . $this->_connect->error); //false means there is an deleting error return false; } //RETURN BOOLEAN } //SELECT DATA FROM DATABASE TABLE public function Get($table, $where=null, $limit=null, $tb_order='DESC'){ //QUERY if ($where == null) { if ($limit == null) { $sql = "SELECT * FROM ".$table." ORDER BY id ".$tb_order.""; }else{ $sql = "SELECT * FROM ".$table." ORDER BY id ".$tb_order." LIMIT ".$limit.""; } }else{ if ($limit == null) { $sql = "SELECT * FROM ".$table." WHERE ".$where." ORDER BY id ".$tb_order." "; }else{ $sql = "SELECT * FROM ".$table." WHERE ".$where." ORDER BY id ".$tb_order." LIMIT ".$limit.""; } } //log array_push($this->_mysqli_logs, "(" . date("Y-m-d H:i:s") . ") Selected Successfully"); $result = $this->_connect->query($sql); $this->_results = $result->fetch_all(MYSQLI_ASSOC); return $this; } //COUNT DATA FROM DATABASE TABLE public function Count($table, $where=null, $limit=null){ //QUERY if ($where == null) { if ($limit == null) { $sql = "SELECT * FROM ".$table.""; }else{ $sql = "SELECT * FROM ".$table." LIMIT ".$limit.""; } }else{ if ($limit == null) { $sql = "SELECT * FROM ".$table." WHERE ".$where." "; }else{ $sql = "SELECT * FROM ".$table." WHERE ".$where." LIMIT ".$limit.""; } } //log array_push($this->_mysqli_logs, "(" . date("Y-m-d H:i:s") . ") Selected Successfully"); $result = $this->_connect->query($sql); return $result->num_rows; //RETURN INT } //GET RESULTS public function results(){ return $this->_results; //RETURN ARRAY } //INSERTED LAST ID public function last_inserted_id(){ return $this->_inserted_last_id; //RETURN ID } //MYSQLI ERROR LOGS public function error_logs($last_log=false){ $all_logs = ''; foreach ($this->_mysqli_logs as $log) { $all_logs .= $log . '<br>'; } return ($last_log) ? end($this->_mysqli_logs) : $all_logs ; //RETURN STRING } public function PreventXSSandSQLinjection($str){ return strip_tags( filter_var(htmlspecialchars( mysqli_real_escape_string($this->_connect, $str), ENT_QUOTES, 'UTF-8' ), FILTER_SANITIZE_STRING) ); } //CLOSE MYSQLI public function __destruct() { mysqli_close($this->_connect); } } ?>
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
<?php /** * Programnas.com * USAGE */ include 'db.php'; //CONNECT $db = new db(); //INSERT if($db->Insert("table_test",array('name' => 'mohammed', 'phone' => '1234567890'))){ echo "Inserted"; } //GET LAST INSERTED ID echo $db->last_inserted_id(); //UPDATE if($db->Update("table_test", array('name' => 'mohammed', 'phone' => '1234567890'), 10)){ echo "Updated"; } //DELETE if($db->Delete("table_test", 1)){ echo "Deleted"; } //SELECT foreach ($db->Get('table_test')->results() as $item) { echo $item["name"].'<br>'; } //COUNT echo $db->Count('table_test'); //ERROR LOGS // Return All Error Logs echo($db->error_logs()); // Return Last Error Log echo($db->error_logs(true)); ?>
Replies
1