Web Analytics Made Easy -
StatCounter Problem with mysql_select_db() and class variable - CodingForum

Announcement

Collapse
No announcement yet.

Problem with mysql_select_db() and class variable

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with mysql_select_db() and class variable

    I am creating a class to work with a knowledge base database. the class only works if I hardcode the database or pass it into each function that works with the database. Below is a scaled down version of the class, any explanation as to why it fails when selecting the database would be appreciated.


    PHP Code:
    class kb
    {
        function 
    kb($server "localhost"$database "KnowledgeBase")
        {
            
    $this->$server $server;
            
    $this->$database $database;
        }

        function 
    ShowOptions()
        {
            
    $link mysql_connect($this->$server)
                or die(
    "Could not connect : " mysql_error());
                
    mysql_select_db($this->$database$link) or die("Could not select database");
        }

        var 
    $server;
        var 
    $database;

    I'm using php 4 still, what gives? Should I update php on my development pc? Is this a bug or am I doing something wrong?

  • #2
    Don't use the dollar sign when accessing with $this:
    $this->$server = $server;
    Use:
    $this->server = $server;

    Good luck;
    -Brando
    Why using tables for eating is stupid!

    Comment


    • #3
      Thanks! I am just getting back into web development and honestly I never did any OOP in php, something I took up with a C# application I've been working on.

      Comment


      • #4
        Follow up question/comment

        That got it working and I can deal without a reply to this, but I am curious and the php manual does not cover working with class variables.

        When debugging I tested:

        $kb = new kb($server, $database);
        echo $kb->$database;


        and that displayed the correct value.

        Any insight?

        Comment


        • #5
          Take a look through here:
          http://www.php.net/manual/en/language.oop.php

          Hope that helps.
          -Brando
          Why using tables for eating is stupid!

          Comment


          • #6
            Thanks

            Originally posted by ensinger View Post
            Should I update php on my development pc?
            Good stuff, it is definately a good idea to update my chm... looks like it is covered now.

            Comment

            Working...
            X