emehrkay Posted March 12, 2006 Share Posted March 12, 2006 so i finally decided to be an adut and put the basic queries into a class and i decided the way that i will parse the data will be from an array. i created a method that will take the array passed in and create the approriate string. and from that string there will be a trailing comma, so i created another method to remove that comma. that works fine for all of the cases in this switch except the "SELECT_FIELDS" onethis is how i call the class and method[code]$arr = array("id","name");$tables = array("test");$where = '';$t = new db_calls('test');$t->select($arr, $tables, $where);[/code]these are the select and result methods[code]public function select($fields, $tables, $where){ $this->parse_array($fields, "SELECT_FIELDS"); $this->parse_array($tables, "SELECT_TABLES"); if($where != ''){ $where_clause = "WHERE " . $where; }else{ $where_clause = ""; } $query = "SELECT " . $this->_db_fields . " FROM " . $this->_db_tables . $where_clause; return $this->result($query); } private function result($query){ $result = mysql_query($query) or die ("<h2>COULD NOT EXECUTE QUERY:</h2><span style=\"color:red;\">".$query."</span><br /><br />".mysql_error()); }[/code]this is the parse array method and remove comma method[code]public function parse_array($arr, $method){ foreach($arr as $key => $value){ switch($method){ case "INSERT": $this->_db_data .= $this->numeric($value) . ", "; $this->_db_fields .= $key . ", "; break; case "SELECT_FIELDS": $this->_db_fields .= $value . ", "; break; case "SELECT_TABLES": $this->_db_tables .= $value . ", "; break; } } if($this->_db_data != '') $this->_db_data = $this->remove_comma($this->_db_data); if($this->_db_fields != '') $this->_db_fields = $this->remove_comma($this->_db_fields); if($this->_db_tables != '') $this->_db_tables = $this->remove_comma($this->_db_tables); } /* * * REMOVE TRAILING COMMA FROM A STRING */ public function remove_comma($string){ $len = strlen($string) -2; //substract two because of the way the comma is added: ", " return substr($string, 0, $len); }[/code]this is what is printed to the screen[code]COULD NOT EXECUTE QUERY:SELECT id, na FROM testUnknown column 'na' in 'field list'[/code]what am i overlooking? for some reason the fields var is removing four chars instead of two. Link to comment https://forums.phpfreaks.com/topic/4733-class-methodfunction-problem/ Share on other sites More sharing options...
keeB Posted March 12, 2006 Share Posted March 12, 2006 [code]COULD NOT EXECUTE QUERY:SELECT id, na FROM testUnknown column 'na' in 'field list'[/code]well, look at how your code is constructed.. and then realize that since it's the 2nd argument, there is no ', ' COMMA SPACE, therefore it's removing the last 2 values in your [b]remove_comma[/b] function.. Link to comment https://forums.phpfreaks.com/topic/4733-class-methodfunction-problem/#findComment-16616 Share on other sites More sharing options...
Barand Posted March 12, 2006 Share Posted March 12, 2006 Why not just join them?[code]$arr = array("id","name");$select_fields = join (', ', $arr);[/code]Eliminates the trailing comma problem. Link to comment https://forums.phpfreaks.com/topic/4733-class-methodfunction-problem/#findComment-16836 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.