Advertisement
Guest User

zipcodechecker2

a guest
Oct 29th, 2011
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. <?php
  2. class mysql {
  3. var $con;
  4. function __construct($db=array()) {
  5. $default = array(
  6. 'host' => 'localhost',
  7. 'user' => 'root',
  8. 'pass' => '',
  9. 'db' => 'test'
  10. );
  11. $db = array_merge($default,$db);
  12. $this->con=mysql_connect($db['host'],$db['user'],$db['pass'],true) or die ('Error connecting to MySQL');
  13. mysql_select_db($db['db'],$this->con) or die('Database '.$db['db'].' does not exist!');
  14. }
  15. function __destruct() {
  16. mysql_close($this->con);
  17. }
  18. function query($s='',$rows=false,$organize=true) {
  19. if (!$q=mysql_query($s,$this->con)) return false;
  20. if ($rows!==false) $rows = intval($rows);
  21. $rez=array(); $count=0;
  22. $type = $organize ? MYSQL_NUM : MYSQL_ASSOC;
  23. while (($rows===false || $count<$rows) && $line=mysql_fetch_array($q,$type)) {
  24. if ($organize) {
  25. foreach ($line as $field_id => $value) {
  26. $table = mysql_field_table($q, $field_id);
  27. if ($table==='') $table=0;
  28. $field = mysql_field_name($q,$field_id);
  29. $rez[$count][$table][$field]=$value;
  30. }
  31. } else {
  32. $rez[$count] = $line;
  33. }
  34. ++$count;
  35. }
  36. if (!mysql_free_result($q)) return false;
  37. return $rez;
  38. }
  39. function execute($s='') {
  40. if (mysql_query($s,$this->con)) return true;
  41. return false;
  42. }
  43. function select($options) {
  44. $default = array (
  45. 'table' => '',
  46. 'fields' => '*',
  47. 'condition' => '1',
  48. 'order' => '1',
  49. 'limit' => 50
  50. );
  51. $options = array_merge($default,$options);
  52. $sql = "SELECT {$options['fields']} FROM {$options['table']} WHERE {$options['condition']} ORDER BY {$options['order']} LIMIT {$options['limit']}";
  53. return $this->query($sql);
  54. }
  55. function row($options) {
  56. $default = array (
  57. 'table' => '',
  58. 'fields' => '*',
  59. 'condition' => '1',
  60. 'order' => '1'
  61. );
  62. $options = array_merge($default,$options);
  63. $sql = "SELECT {$options['fields']} FROM {$options['table']} WHERE {$options['condition']} ORDER BY {$options['order']}";
  64. $result = $this->query($sql,1,false);
  65. if (empty($result[0])) return false;
  66. return $result[0];
  67. }
  68. function get($table=null,$field=null,$conditions='1') {
  69. if ($table===null || $field===null) return false;
  70. $result=$this->row(array(
  71. 'table' => $table,
  72. 'condition' => $conditions,
  73. 'fields' => $field
  74. ));
  75. if (empty($result[$field])) return false;
  76. return $result[$field];
  77. }
  78. function update($table=null,$array_of_values=array(),$conditions='FALSE') {
  79. if ($table===null || empty($array_of_values)) return false;
  80. $what_to_set = array();
  81. foreach ($array_of_values as $field => $value) {
  82. if (is_array($value) && !empty($value[0])) $what_to_set[]="`$field`='{$value[0]}'";
  83. else $what_to_set []= "`$field`='".mysql_real_escape_string($value,$this->con)."'";
  84. }
  85. $what_to_set_string = implode(',',$what_to_set);
  86. return $this->execute("UPDATE $table SET $what_to_set_string WHERE $conditions");
  87. }
  88. function insert($table=null,$array_of_values=array()) {
  89. if ($table===null || empty($array_of_values) || !is_array($array_of_values)) return false;
  90. $fields=array(); $values=array();
  91. foreach ($array_of_values as $id => $value) {
  92. $fields[]=$id;
  93. if (is_array($value) && !empty($value[0])) $values[]=$value[0];
  94. else $values[]="'".mysql_real_escape_string($value,$this->con)."'";
  95. }
  96. $s = "INSERT INTO $table (".implode(',',$fields).') VALUES ('.implode(',',$values).')';
  97. if (mysql_query($s,$this->con)) return mysql_insert_id($this->con);
  98. return false;
  99. }
  100. function delete($table=null,$conditions='FALSE') {
  101. if ($table===null) return false;
  102. return $this->execute("DELETE FROM $table WHERE $conditions");
  103. }
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement