Posted By

lajevardi on 03/01/09


Tagged

mysql database class oop security connection


Versions (?)

Who likes this?

12 people have marked this snippet as a favorite

kistank
jadedbat
nkrstevski
vali29
krike
tomthorgal
andrewbowley
TheJasonParker
prit
tewoos
MichaelM
rusty_richards


Simple Database Class


 / Published in: PHP
 

That's not complete enough! but as useful as a knife :/

  1. <?php
  2. class Database{
  3. private $host;
  4. private $user;
  5. private $pwd;
  6. private $rows;
  7. private $error;
  8. private $result;
  9. private $dbName;
  10. private $connection;
  11. private $isReady;
  12.  
  13. public function __construct(){
  14. $this->result = null;
  15. $this->isReady = false;
  16. $this->error = array();
  17. }
  18. public function __destruct(){ @mysql_close($this->connection); }
  19.  
  20. /* setters */
  21. public function setHost($host){ $this->host = $host; }
  22. public function setUser($user){ $this->user = $user; }
  23. public function setPassword($pwd){ $this->pwd = $pwd; }
  24. public function setDbName($dbName){ $this->dbName = $dbName; }
  25.  
  26. /* other interfaces */
  27. public function init($host=null,$user=null,$pwd=null,$dbName=null){
  28. if(!isset($host,$user,$pwd,$dbName))
  29. die("Please provide require settings.");
  30. $this->setHost($host);
  31. $this->setUser($user);
  32. $this->setPassword($pwd);
  33. $this->setDbName($dbName);
  34. $this->isReady = true;
  35. }
  36.  
  37. public function select($dbName){
  38. $this->setDbName($dbName);
  39. mysql_select_db($this->dbName,$this->connection) or die("The said database does not exist.");
  40. }
  41.  
  42. public function query($sql){
  43. $this->result = mysql_query($sql,$this->connection) or die("Invalid query string!");
  44. }
  45.  
  46. public function connect(){
  47. if(!$this->isReady) die("not ready to connect");
  48. $this->connection = mysql_connect($this->host,$this->user,$this->pwd) or die("Could not connect to database. please check your credentials.");
  49. $this->select($this->dbName);
  50. $this->query("SET NAMES 'utf8'",$this->connection); //persian support
  51. }
  52.  
  53. public function isConnected(){
  54. if($this->connection)
  55. return true;
  56. return false;
  57. }
  58.  
  59. public function disconnect(){
  60. mysql_close($this->connection);
  61. $this->connection = null;
  62. }
  63.  
  64. public function countRows($selectMode = true){
  65. if($selectMode)
  66. return mysql_num_rows($this->result);
  67. return mysql_affected_rows($this->connection);
  68. }
  69.  
  70. public function loadRows(){
  71. if(!$this->result) die("Nothing found!");
  72. $this->rows = array();
  73. while($r = mysql_fetch_array($this->result,MYSQL_BOTH))
  74. $this->rows[] = $r;
  75. mysql_free_result($this->result);
  76. return $this->rows;
  77. }
  78.  
  79. public function siftDown($dataStack){
  80. if(!is_array($dataStack)){
  81. $dataStack = ereg_replace("[\'\")(;|`,<>]","",$dataStack);
  82. $dataStack = mysql_real_escape_string(trim($dataStack),$this->connection);
  83. $dataStack = stripslashes($dataStack);
  84. return $dataStack;
  85. }
  86. $safeData = array();
  87. foreach($dataStack as $p=>$data){
  88. $data = ereg_replace("[\'\")(;|`,<>]","",$data);
  89. $data = mysql_real_escape_string(trim($data),$this->connection);
  90. $data = stripslashes($data);
  91. $safeData[$p] = $data;
  92. }
  93. return $safeData;
  94. }
  95.  
  96. public function secure($data){
  97. return sha1(md5(sha1(md5(sha1($data)))));
  98. }
  99. }//Database class
  100. ?>
  101.  
  102. <?php //usage
  103. require_once 'path/to/Database.class.php';
  104. $db = new Database(); //Creating new object
  105. $db->init("localhost","test_root","test_pwd!","test_db"); //initializing by credentials.
  106. $db->connect(); //unicode support
  107. $test_value = $db->siftDown($test_value); //preventing harmful inputs
  108. $something_testy_else = $db->siftDown($something_testy_else);
  109. $db->query("SELECT * FROM test_table WHERE test_field = '$test_value' AND second_test_field = '$something_testy_else' LIMIT 1");
  110. if($db->countRows()==1)
  111. $dbdata = $db->loadRows(); //returns a numeric/associative array as the result (MYSQL_BOTH)
  112. //TODO: To Process $dbdata
  113. $db->disconnect();
  114. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: jadedbat on March 2, 2009

I am new to classes can you show use examples. :)

Posted By: lajevardi on March 3, 2009

Sure, take a look at this:

Posted By: lajevardi on March 3, 2009

Posted By: lajevardi on March 3, 2009

@jadedbat ah! this commenting has derived me mad, I'll update the snippet.

Posted By: jadedbat on March 28, 2009

Sweet! tnx!

Posted By: AskoJr on October 8, 2010

Thanks mate. Works for me.

You need to login to post a comment.