Jump to content

Archived

This topic is now archived and is closed to further replies.

emehrkay

class method/function problem

Recommended Posts

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" one

this 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 test

Unknown column 'na' in 'field list'[/code]

what am i overlooking? for some reason the fields var is removing four chars instead of two.

Share this post


Link to post
Share on other sites

[code]
COULD NOT EXECUTE QUERY:
SELECT id, na FROM test

Unknown 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..

Share this post


Link to post
Share on other sites
Why not just join them?

[code]$arr = array("id","name");

$select_fields = join (', ', $arr);[/code]

Eliminates the trailing comma problem.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.