I feel really stupid. I'm trying to use OOP for the first time and design a class that handles database functions. I started off with this code and for some reason it won't work.

<?

class mysql {

	var $host;
	var $database;
	var $username;
	var $password;

function mysql()

	{

	mysql_connect($this->host, $this->username, $this->password)
		or die("<b>Could not connect to MySQL server:</b><br>" . mysql_error());

	mysql_select_db($this->db)
		or die("<b>Could not select MySQL database:</b><br>" . mysql_error();

	return;

	}

}

?> 

Any Ideas?

    As posted it dows not appear that you are setting the values of $host,$username etc.

    Abd you have $this->db instead of $this->database

      Sorry, I posted the wrong code:

      Here is the class file:

      <?
      
      class mysql {
      
      	var $host;
      	var $database;
      	var $username;
      	var $password;
      
      function mysql()
      
      	{
      
      
      
      
      	mysql_connect($this->host, $this->username, $this->password)
      		or die("<b>Could not connect to MySQL server:</b><br>" . mysql_error());
      
      			mysql_select_db($this->database)
      		or die("<b>Could not select MySQL database:</b><br>" . mysql_error());
      
      
      
      	return;
      	}
      
      }
      
      ?> 

      Here is the file that sets the variables:

      <?
      
      include('mysql.php');
      
      
      
      $test = new mysql;
      
      $test->host = "localhost234";
      $test->username = "root";
      $test->password = "";
      $test->database = "TEST";
      
      
      
      ?>
      

      I put the wrong values for the variables to see if the error handling worked and if the function works.

      When I run the page that calls the class it gives me this error:

      Could not select MySQL database:
      No Database Selected

      The mysql_connect function does not seem to be working in the class.

      And yes, MySQL works on my computer. I just can't seem to get it to work with this class.

      What am I doing wrong?

        The constructor (function mysql()) gets called as soon as you new it. So the values have not been set yet.

        You either need to pass them into the constructor or move the connect code to it's own method.

          Any reason the variables are being store in a separate file outside the class? I always put my variables at the top of a config page, the class beneath it, and then call that page as an include. For example, let's say I had a page called "config.php"

          <? 
          $dbUser = "user";
          $dbPass = "pass";
          $dbName = "name";
          $dbHost = "host";
          
          class DBClass
          {
               var $dbUser;
               var $dbPass;
               var $dbName;
               var $dbHost;
          
           function DBClass()
          {
              $this->dbUser = $GLOBALS["dbUser"];
              $this->dbPass = $GLOBALS["dbPass"];
              $this->dbName = $GLOBALS["dbName"];
              $this->dbName = $GLOBALS["dbHost"];
          
              // connection code goes here
           }
          }
          

          Then I would include config.php from any page I needed to use it from. My classes are usually a lot different than this, but you should be able to get the idea.

            Write a Reply...