Jump to content

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.
Link to comment
https://forums.phpfreaks.com/topic/4733-class-methodfunction-problem/
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..
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.