I am having a little problem. I am using Apache 2 on Windows XP with MySQL and PHP 5.0.2
I started to work with object oriented programming and am having a problem. I am confused since it seems that my code should work.
Here is the code in my MySQL.php (usernames\passwords edited of course 🙂 ) file:
// MySQL Database class
class MySQL
{
var $username;
var $password;
var $db_name;
// Constructor
function MySQL ($username,$password,$db_name)
{
$this->username = $username;
$this->password = $password;
$this->db_name = $db_name;
} // END MySQL Function
// Method to connect to a MySQL database
function DB_Connect ()
{
$conn = mysql_connect("localhost",$this->username,$this->password) or die("\n<strong>Could not connect to the database.</strong>\n");
$tmp = MySQL::DB_Select($conn);
if (mysql_error($conn))
{
print "An error has occured.\n".
"Please notify the computer administrator of the following error:\n".
mysql_error($conn);
exit;
} // END IF
return $conn;
} // END DB_Connect Function
// Method to select the MySQL database to open
function DB_Select ($conn)
{
mysql_select_db($this->db_name,$conn);
} // END DB_Select Function
// Method to close database connections
function DB_Close ()
{
mysql_close();
} // END DB_Close Function
// Method to query MySQL database
function DB_Query ()
{
} // END DB_Query Function
} // END MySQL Class
And here is the code on my main page:
require_once("MySQL.php");
$username = "username";
$password = "password";
$db_name = "nameofDB";
$conn = new MySQL($username,$password,$db_name);
$conn = MySQL::DB_Connect();
Here is the error I get:
Fatal error: Using $this when not in object context in MySQL.php on line 20
NOTE: Line 20 is in the DB_Connect() method; the line that calls the mysql_connect() function.
Also note that were it adds the <img> tag in the code is really supposed to be a second : (colon).
Any help would be appreciated.