Jump to content

[SOLVED] Fatal error: Cannot access empty property... Please help me out on this one


Pastulio

Recommended Posts

I have created a class that allows you to create a database by calling the class.

 

The problem is I get 2 errors:

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\classes\create_db.php on line 30

 

and

 

Fatal error: Cannot access empty property in C:\wamp\www\blog\classes\create_db.php on line 30

 

I don't know why I'm having this problem because there is no password on my local machine for the MySQL database, and I've clearly set the username when I call the class.

 

Here is the exact code:

 

create_db.php

<?php

/*	+---------------------------------------------------+
|                                                   |
|       PHP MySQL database creation class           |
|                                                   |
+---------------------------------------------------+
| Filename   : create_db.php                        |
| Created    : 25/06/2007 20:29                     |
| Created By : Pascal Van Acker (a.k.a Pastulio)    |
| Email      :                                      |
| Version    : 1.0                                  |
|                                                   |
+---------------------------------------------------+	*/

class create_database {

// MySQL config
var $db_host;              // MySQL host (usually 'localhost')
var $db_username;       // MySQL username
var $db_password;       // MySQL password
var $database;          // MySQL database name
var $connection;        // MySQL connection variable
var $db_select;         // MySQL selection variable
var $db_table;          // MySQL table selection

function db_Connect () {

	// Connect to the MySQL server
	$this -> $connection = mysql_connect ($this -> db_host, $this -> db_username, $this -> db_password);

		// Test the MySQL connection
		if (!$this -> connection) { return false; }

} // END db_Connect

function db_Disconnect () {

	mysql_close ($this -> connection);

} // END db_Disconnect

function check_Existence () {

	$result = mysql_list_dbs ($this -> connection);

	while ($list = mysql_fetch_array($result)) {

		$database_list[] =  $list['database'];

	}

	if (in_array($this -> database, $database_list)) {

		return true;

	} else {

		return false;

	}

}

function db_Create () {

	$query = "CREATE DATABASE `$this -> database`";
	$result = mysql_query ($query);

	if (!$result) { return false; } else { return true; }

} // END db_Create

function create_database () {

	if (!$this -> db_Connect()){ return false; }
	if ($this -> check_Existence ()) {

		return false;

	} else {

		if ($this -> db_Create()){

			return true;

		} else {

			return false;

		}

	}


}

}

/*		THE CODE TO INCLUDE IN THE FILE WHERE YOU NEED TO CALL THE DATABASE CREATION		*/

$create_db = new create_database ();              // Call the class
$create_db -> host = 'localhost';                 // Set the MySQL host
$create_db -> db_username = 'root';               // Set the MySQL username
$create_db -> db_password = '';                   // Set the MySQL password
$create_db -> database = 'test_database_01';      // Set the MySQL to be created database name
$create_db -> create_database();
$create_db -> db_Disconnect();

?>

Ok I've edited it and Found out that I named the create_database function the same as the function, which would make it a constructor and run that first.

I've changed it and it works but the 2nd error is still there

 

Fatal error: Cannot access empty property in C:\wamp\www\blog\classes\create_db.php on line 30

 

This is line 30

$this -> $connection = mysql_connect ($this -> db_host, $this -> db_username, $this -> db_password);

 

Any help would be greatly appreciated

 

This is line 30

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.