0

I am fairly new at PHP OOP but the following piece of PHP code is returning the following error and I have no idea why. If someone could point out what is probably an obvious error I would be most grateful

Fatal error: Call to undefined function get_column_names() in C:\tester

class tester
{
    public $database_name;
    public $auto_increment_col; 
    public $foreign_key_array;
    public $column_names;

    function __construct($wpdb,$tablename) 
    {
        $this->database_name        = get_database_name();
        $this->auto_increment_col   = get_auto_increment_field($tablename);
        $this->foreign_key_array    = get_foreign_keys($tablename);
        //$this->column_names       = $wpdb->get_results( "SHOW COLUMNS FROM $tablename");
        $this->column_names         = get_column_names($wpdb,$tablename);
    }

    protected function get_column_names($wpdb,$tablename)
    {
        return $wpdb->get_results( "SHOW COLUMNS FROM $tablename"); 
    }

    protected function get_database_name() 
    {
        $r = mysql_query("SELECT DATABASE()") or die(mysql_error());
        return mysql_result($r, 0);
    }

    protected function get_auto_increment_field($tablename)
    {
        $sql = "describe $tablename";
        $result = mysql_query($sql);
        while ($row = mysql_fetch_assoc($result))
        {
            if ($row['Extra'] == 'auto_increment')
            {
                return($row['Field']);
            }
        }
        return(null);
    }
    protected function get_foreign_keys($wpdb,$tablename)
    {
        $foreign_keys = array();

        $sql = "select *
          FROM information_schema.TABLE_CONSTRAINTS i
          LEFT JOIN information_schema.KEY_COLUMN_USAGE k ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME
          WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY'
          AND i.TABLE_SCHEMA = DATABASE()
          AND i.TABLE_NAME = '$tablename'";

        $rows = $wpdb->get_results($sql);

        if (count($rows) == 0)
            return(null);
        else
            return($rows);
    }




}
0

1 Answer 1

4

You are missing the $this when calling object methods:

$this->column_names = $this->get_column_names($wpdb,$tablename);
// ... and so on
Sign up to request clarification or add additional context in comments.

5 Comments

Good answer ... what's with the "Damn!"?
I'm note a native speaker.. Didn't wanted to be offensive. Just wanted to point out that the solution was really easy to find
Thanks very much for your answer - what you said did work. However, what puzzles me is that the other statements like get_foreign_keys($tablename); worked fine without the $this->
The only reason for this that comes in mind is that function might have been declared globally before. Try var_dump(function_exists('get_foreign_keys'));
Thanks for the explanation. I think your post is improved with the "damn" taken away. Thanks again for the great answer. +1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.