Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
[PHP, MySQLi] Database Class
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="iExit" data-source="post: 332212" data-attributes="member: 33201"><p>Hey.</p><p>I created a Database System which should be very save.</p><p>Code: [code]</p><p>class Database {</p><p> </p><p> private $connection;</p><p> private $executedQuerys = [];</p><p> </p><p> public function __construct(\mysqli $connection) {</p><p> $this->connection = $connection;</p><p> }</p><p> </p><p> public function query($query, $params = [], $callback = null) {</p><p> $query = $this->getConvertedQueryString($query, $params);</p><p> </p><p> if($callback == null || $callback() == true) {</p><p> $return = $this->connection->query($query) or die($this->connection->error);</p><p> } else {</p><p> return null;</p><p> }</p><p> </p><p> $this->executedQuerys[] = [</p><p> 'query' => $query, </p><p> 'data' => $return</p><p> ]; </p><p> </p><p> return $return;</p><p> }</p><p> </p><p> public function multiQuery($querys = []) {</p><p> $result = [];</p><p> foreach($querys as $query) {</p><p> $qry = $query['query'];</p><p> $params = isset($query['params']) ? $query['params'] : [];</p><p> $callback = isset($query['callback']) ? $query['callback'] : null;</p><p> </p><p> $result[] = $this->query($qry, $params, $callback);</p><p> }</p><p> </p><p> return $result;</p><p> }</p><p> </p><p> private function getConvertedQueryString($query, $params = []) {</p><p> $toFind = [];</p><p> $toReplace = [];</p><p> </p><p> foreach($params as $key => $value) {</p><p> $toFind[] = ':'.$key;</p><p> $toReplace[] = "'".$this->connection->real_escape_string($value)."'";</p><p> }</p><p> $query = str_replace($toFind, $toReplace, $query);</p><p> </p><p> return $query;</p><p> }</p><p> </p><p> public function getExecutedQuerys() {</p><p> return $this->executedQuerys;</p><p> }</p><p>}</p><p>[/code]</p><p>Example: [code]</p><p>$db = new Database(new MySQLi('localhost', 'root', '', 'test'));</p><p></p><p></p><p>$user = $db->query('SELECT * FROM users WHERE username = :username AND id = :id', [</p><p> 'username' => 'Test',</p><p> 'id' => 1</p><p>], function() use ($db) {</p><p> return $db->query('SELECT * FROM users WHERE username = :username', ['username' => 'Test'])->num_rows == 0;</p><p>});</p><p></p><p>echo $multiQuery = $db->multiQuery([</p><p> [</p><p> 'query' => 'SELECT * FROM users WHERE username = :username AND id = :id', </p><p> 'params' => [</p><p> 'username' => 'iExit',</p><p> 'id' => 1</p><p> ]</p><p> ],</p><p> [</p><p> 'query' => 'INSERT INTO users SET username = :username',</p><p> 'params' => [</p><p> 'username' => 'Test1231'</p><p> ],</p><p> </p><p> 'callback' => function() use ($db) {</p><p> //callback :D </p><p> return $db->query('SELECT * FROM users WHERE username = :username', ['username' => 'Test1231'])->num_rows == 0; </p><p> }</p><p> ]</p><p> </p><p>])[0]->fetch_object()->username; // returns "iExit" :D</p><p>[/code]</p><p></p><p>Why should you use it?</p><p>- save querys</p><p>- multi-querys</p><p>- callbacks (query execute when callback returns boolean "true")</p><p></p><p>If u got a question just ask.</p><p></p><p>Cya</p></blockquote><p></p>
[QUOTE="iExit, post: 332212, member: 33201"] Hey. I created a Database System which should be very save. Code: [code] class Database { private $connection; private $executedQuerys = []; public function __construct(\mysqli $connection) { $this->connection = $connection; } public function query($query, $params = [], $callback = null) { $query = $this->getConvertedQueryString($query, $params); if($callback == null || $callback() == true) { $return = $this->connection->query($query) or die($this->connection->error); } else { return null; } $this->executedQuerys[] = [ 'query' => $query, 'data' => $return ]; return $return; } public function multiQuery($querys = []) { $result = []; foreach($querys as $query) { $qry = $query['query']; $params = isset($query['params']) ? $query['params'] : []; $callback = isset($query['callback']) ? $query['callback'] : null; $result[] = $this->query($qry, $params, $callback); } return $result; } private function getConvertedQueryString($query, $params = []) { $toFind = []; $toReplace = []; foreach($params as $key => $value) { $toFind[] = ':'.$key; $toReplace[] = "'".$this->connection->real_escape_string($value)."'"; } $query = str_replace($toFind, $toReplace, $query); return $query; } public function getExecutedQuerys() { return $this->executedQuerys; } } [/code] Example: [code] $db = new Database(new MySQLi('localhost', 'root', '', 'test')); $user = $db->query('SELECT * FROM users WHERE username = :username AND id = :id', [ 'username' => 'Test', 'id' => 1 ], function() use ($db) { return $db->query('SELECT * FROM users WHERE username = :username', ['username' => 'Test'])->num_rows == 0; }); echo $multiQuery = $db->multiQuery([ [ 'query' => 'SELECT * FROM users WHERE username = :username AND id = :id', 'params' => [ 'username' => 'iExit', 'id' => 1 ] ], [ 'query' => 'INSERT INTO users SET username = :username', 'params' => [ 'username' => 'Test1231' ], 'callback' => function() use ($db) { //callback :D return $db->query('SELECT * FROM users WHERE username = :username', ['username' => 'Test1231'])->num_rows == 0; } ] ])[0]->fetch_object()->username; // returns "iExit" :D [/code] Why should you use it? - save querys - multi-querys - callbacks (query execute when callback returns boolean "true") If u got a question just ask. Cya [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
[PHP, MySQLi] Database Class
Top