{"id":130,"date":"2010-06-20T15:05:11","date_gmt":"2010-06-20T20:05:11","guid":{"rendered":"http:\/\/jamesroberts.name\/blog\/?p=130"},"modified":"2010-06-20T15:05:55","modified_gmt":"2010-06-20T20:05:55","slug":"simple-but-useful-php-base-object-for-mysql-table-interaction","status":"publish","type":"post","link":"https:\/\/jamesroberts.name\/blog\/2010\/06\/20\/simple-but-useful-php-base-object-for-mysql-table-interaction\/","title":{"rendered":"simple but useful php base object for mysql table interaction"},"content":{"rendered":"<p>I started a project creating an online game which involved lots of objects and tables.  Rather than rewrite the same code over and over, I decided to create a base object in which i could simple extend the base object to work with any mySQL table by simply listing all the fields.<\/p>\n<p>Here is the code for the base object:<\/p>\n<pre lang=\"php\">\r\n<?\r\nabstract class _base\r\n{\r\n\t\r\n\tprotected $db = null;\r\n\tprotected $table = '';\r\n\tprotected $primary_field = '';\r\n\tprotected $primary_id = -1;\r\n\tprotected $fields = array();\r\n\tprotected $values = array();\r\n\t\r\n\tprivate $db_info = array(\r\n\t\t'host'=>'localhost',\r\n\t\t'username'=>'',\r\n\t\t'password'=>'',\r\n\t\t'database'=>''\r\n\t);\r\n\t\r\n\tpublic function __construct($primary_id=-1)\r\n\t{\r\n\t\r\n\t\t$this->db = $this->db_connect();\r\n\t\t\r\n\t\tif($this->primary_id>0){\r\n\t\t\t$this->primary_id = $primary_id;\r\n\t\t\t$this->load($this->primary_id);\r\n\t\t}\r\n\t\t\r\n\t\treturn true;\r\n\t}\r\n\t\r\n\tpublic function load($primary_id=-1)\r\n\t{\r\n\t\tif($primary_id>0) $this->primary_id = $primary_id;\r\n\t\t\r\n\t\tif($this->primary_id<0) return false;\r\n\t\t\r\n\t\t$sql = \"SELECT * FROM \".$this->table.\" WHERE \".$this->primary_field.\"='\".intval($this->primary_id).\"' LIMIT 1\";\r\n\t\t$result = $this->db->query($sql);\r\n\t\tif($this->db->errno) echo '<div class=\"red b ac\">Query Fail ('.$this->db->error.')<br>sql: '.$sql.'<\/div>';\r\n\t\t\r\n\t\tif($result && $result->num_rows>0){\r\n\t\t\t$tmp_vals = $result->fetch_assoc();\r\n\t\t\t$this->values = $tmp_vals;\r\n\t\t\tforeach($tmp_vals as $k=>$v){\r\n\t\t\t\t$this->$k = $v;\r\n\t\t\t}\r\n\t\t\treturn true;\r\n\t\t}else{\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\tpublic function load_from_array($load_array = array())\r\n\t{\r\n\t\tforeach($load_array as $field=>$value){\r\n\t\t\t$this->$field = $value;\r\n\t\t}\r\n\t\treturn true;\r\n\t}\r\n\r\n\tpublic function save()\r\n\t{\r\n\t\t$primary_field = $this->primary_field;\r\n\t\t\r\n\t\t$save_new = true;\r\n\t\t\r\n\t\tif($this->$primary_field>0) $save_new = false;\r\n\t\t\r\n\t\t$this->prepare_save();\r\n\t\t\r\n\t\tif($save_new){\r\n\t\t\r\n\t\t\t$sql = \"INSERT INTO \".$this->table;\r\n\t\t\t$fields = \"(\".implode(',',array_keys($this->values)).\")\";\r\n\t\t\t$values = \"('\".implode(\"','\",array_values($this->values)).\"')\";\r\n\t\t\t$sql .= ' '.$fields. ' VALUES '.$values; \r\n\t\t\t\r\n\t\t}else{\r\n\t\t\t\r\n\t\t\t$sql = \"UPDATE \".$this->table.\" SET \";\r\n\t\t\t$set = '';\r\n\t\t\tforeach ($this->values as $k => $v){\r\n\t\t\t\t$set.=(strlen($set)?', ':'').$k.\"='\".$v.\"'\";\r\n\t\t\t}\r\n\t\t\t$sql .= $set.\" WHERE \".$primary_field.\"=\".intval($this->primary_id);\r\n\t\t}\r\n\t\t\r\n\t\t$result = $this->db->query($sql);\r\n\t\tif($this->db->errno) echo '<div class=\"red b ac\">Query Fail ('.$this->db->error.')<br>sql: '.$sql.'<\/div>';\r\n\t\t\r\n\t\tif($result){\r\n\t\t\t\r\n\t\t\tif($save_new) $this->$primary_field = $this->db->insert_id;\r\n\t\t\t\r\n\t\t\treturn true;\r\n\t\t}else{\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\t\r\n\tprotected function prepare_save()\r\n\t{\r\n\t\t\r\n\t\t$this->values = array();\r\n\t\tforeach($this->fields as $field){\r\n\t\t\tif(isset($this->$field)) $this->values[$field] = $this->db->real_escape_string($this->$field);\r\n\t\t}\r\n\t\tunset($this->values[$this->primary_field]);\r\n\t}\r\n\t\r\n\tprotected function set_or_get($key='',$value=null)\r\n\t{\r\n\t\tif(!strlen($key)) return false;\r\n\t\t\r\n\t\tif(is_null($value) || (is_string($value) && !strlen($value)) || (is_int($value) && $value < 0)){\r\n\t\t\treturn $this->$key;\r\n\t\t}else{\r\n\t\t\t$this->$key = $value;\r\n\t\t\treturn true;\r\n\t\t}\r\n\t}\r\n\r\n\tpublic function set($key='',$value=null)\r\n\t{\r\n\t\tif(!strlen($key) || is_null($value)) return false;\r\n\t\tif(!in_array($key,$this->fields)) return false;\r\n\t\t\r\n\t\t$this->$key = $value;\r\n\t\t\r\n\t}\r\n\t\r\n\tpublic function get($key='')\r\n\t{\r\n\t\tif(!strlen($key)) return false;\r\n\t\tif(!in_array($key,$this->fields)) return false;\r\n\t\treturn $this->$key;\r\n\t}\r\n\t\r\n\tpublic function object_info()\r\n\t{\r\n\t\t$object_info = array();\r\n\t\t$object_info['primary_field'] = $this->primary_field;\r\n\t\t$object_info['table'] = $this->table;\r\n\t\t$object_info['fields'] = $this->fields;\r\n\t\t\r\n\t\treturn $object_info;\r\n\t}\r\n\t\r\n\tpublic function db_connect(){\r\n\t\t$this->db = new mysqli($this->db_info['host'],$this->db_info['username'],$this->db_info['password'],$this->db_info['database']);\r\n\t\treturn $this->db;\r\n\t}\r\n\t\r\n}\r\n?>\r\n<\/pre>\n<p>To extend the object to use a certain mysql table, you simply set the table name, list the name of the fields, and the primary key.  Then you ca access all the values of the mysql table with the variable name of the field names&#8230;. here is an example:<\/p>\n<pre lang=\"php\">\r\nclass user extends _base\r\n{\r\n\tprotected $primary_field = 'user_id';\r\n\tprotected $table = 'users';\r\n\tprotected $fields = array(\r\n\t\t'user_id ',\r\n\t\t'username',\r\n\t\t'password'\r\n\t);\r\n\t\r\n\tprotected $user_id  = -1;\r\n\tprotected $username  = '';\r\n\tprotected $password  = '';\r\n\t\r\n\tpublic function __construct($user_id=-1)\r\n\t{\r\n\t\r\n\t\tparent::__construct();\r\n\t\t\r\n\t\tif($user_id>0){\r\n\t\t\t$this->user_id = $user_id;\r\n\t\t\t$this->load($user_id);\r\n\t\t}\r\n\t\t\r\n\t\treturn true;\r\n\t}\r\n\t\r\n\tpublic function login($username='', $password='')\r\n\t{\r\n\t\tif(!strlen($username) || !strlen($password)) return false;\r\n\t\t$sql = \"SELECT user_id FROM users WHERE username='\".$this->db->real_escape_string($username).\"' AND password='\".$this->db->real_escape_string($password).\"'\";\r\n\t\t$result = $this->db->query($sql);\r\n\t\tif($this->db->errno) echo '<div class=\"red b ac\">Query Fail ('.$this->db->error.')<br>sql: '.$sql.'<\/div>';\r\n\t\tif($result && $result->num_rows > 0){\r\n\t\t\t$user_data = $result->fetch_assoc();\r\n\t\t\t$this->load($user_data['user_id']);\r\n\t\t\t\r\n\t\t\t\/\/ do user login stuff\r\n\r\n\t\t\treturn true;\r\n\t\t}else{\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\t\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I started a project creating an online game which involved lots of objects and tables. Rather than rewrite the same code over and over, I decided to create a base object in which i could simple extend the base object to work with any mySQL table by simply listing all the fields. Here is the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[32,65,31,62],"_links":{"self":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/130"}],"collection":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/comments?post=130"}],"version-history":[{"count":2,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":132,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/130\/revisions\/132"}],"wp:attachment":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}