/mysql.php

https://github.com/smartarts/php-class-mysql · PHP · 115 lines · 87 code · 26 blank · 2 comment · 11 complexity · b8504d9f5412f8bb331796bae51a179a MD5 · raw file

  1. <?
  2. class mysql{
  3. //internal vars
  4. protected $dbid;
  5. protected $type;
  6. protected $fields="*";
  7. protected $table;
  8. protected $query;
  9. protected $where;
  10. protected $order;
  11. protected $limit;
  12. protected $values;
  13. protected $dbvar=array();
  14. protected $expvar=array();
  15. //external vars
  16. public $error;
  17. public $result;
  18. public $rows=array();
  19. public $numrows;
  20. public $sql;
  21. public $insertid;
  22. public $status;
  23. public $cntr=0;
  24. function __construct($dbid=1){
  25. if($dbid==1) $this->connect(DBUSER,DBPASS,DBHOST,DBNAME);
  26. }
  27. function connect($username,$password,$host,$db){
  28. $connect=mysql_connect($host,$username,$password) or die("db fail");
  29. $db=mysql_select_db($db,$connect);
  30. }
  31. function fields($x){
  32. $this->set($x);
  33. $this->fields="";
  34. foreach($this->dbvar as $v){
  35. $this->fields.=$seperate.$v;
  36. $seperate=",";
  37. }
  38. }
  39. function table($x){
  40. $this->table=$x;
  41. }
  42. function values($x){
  43. if($this->values=='') $this->values=$x;
  44. else $this->values.=",".$x;
  45. }
  46. function where($x){
  47. if($this->where=='') $this->where=$x;
  48. else $this->where.=" AND ".$x;
  49. }
  50. function order($x){
  51. $this->order=$x;
  52. }
  53. function limit($x){
  54. $this->limit=$x;
  55. }
  56. function set($x){
  57. $tmp=explode(",",$x);
  58. foreach($tmp as $t){
  59. $parts=explode("=",$t);
  60. if($parts[1]=='') $parts[1]=$parts[0];
  61. array_push($this->dbvar,$parts[0]);
  62. array_push($this->expvar,$parts[1]);
  63. }
  64. }
  65. function get($x){
  66. foreach($x as $field => $value){
  67. if(in_array($field,$this->dbvar)){
  68. $key=array_keys($this->dbvar,$field);
  69. $field=$this->expvar[$key[0]];
  70. }
  71. $GLOBALS[$field]=$value;
  72. $this->$field=$value;
  73. }
  74. $this->cntr++;
  75. }
  76. function status(){
  77. if($this->status==TRUE) echo "<div class='ok'>Update OK</div>"; else echo "<div class='error'>Update Failed</div>";
  78. }
  79. function debug($x){
  80. if($x==0) echo "<div>".$this->query."</div>";
  81. else mail("andrew@smartarts.co.uk","SQL Debug",$this->query,"From: debug@smartarts.co.uk");
  82. }
  83. }
  84. require(getdir(__FILE__)."select.php");
  85. require(getdir(__FILE__)."insert.php");
  86. require(getdir(__FILE__)."update.php");
  87. require(getdir(__FILE__)."delete.php");
  88. require(getdir(__FILE__)."string.php");
  89. ?>