Jump to content

Recommended Posts

Ok, so I have a class to connect to a database (and it would be done much easier as just two functions).

 

here is the script:

 

connect_db.php

<?php

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

class db_connect {

// MySQL Config
var $db_host;           // MySQL host (usually 'localhost')
var $db_username;       // MySQL username
var $db_password;       // MySQL password
var $db_name;			// MySQL database name
var $db_connection;		// MySQL database connection variable
var $db_select;			// MySQL database selection variable

function db_Connect () {

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

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

} // END db_Connect

function db_Select () {

	$this -> db_select = mysql_select_db ($this -> database, $this -> db_connection);

		// Test the MySQL selection
		if (!$this -> db_select) { return false; } else { return true; }

}

}

/*

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

$create_db = new db_connect ();     // Call the class
$create_db -> db_host = '';         // Set the MySQL host
$create_db -> db_username = '';     // Set the MySQL username
$create_db -> db_password = '';     // Set the MySQL password
$create_db -> database_name = '';   // Set the MySQL to be created database name
$create_db -> db_Connect ();
$create_db -> db_Select ();

*/

?>

 

But I want other classes to be able to use that function to do that.

Here is another class in which I need it:

 

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 $database_name;          // MySQL database name

function check_Existence () {

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

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

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

	}

	if (!in_array($this -> database_name, $database_list)) {

		return true;

	} else {

		return false;

	}

}

function db_Create () {

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

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

} // END db_Create

function create () {

	if ($this -> db_Connect ()){

		if ($this -> check_Existence ()){

			if ($this -> db_Create ()){

				return true;

			}

		}

	}


}

}

/*

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 -> database_name = '';             // Set the MySQL to be created database name
$create_db -> create();

*/



?>

 

Now can anybody help me on how to do this or should I just call the connect_db class in the pages I need manually? (in which case I will just make the class into 2 functions)

 

Thanks a lot,

Pastulio

Link to comment
https://forums.phpfreaks.com/topic/57174-inheritance-need-help/
Share on other sites

Ok so this is what I did

 

 

database_classes.php

<?php

// Class db_connect

class db_connect {

// MySQL Config
var $db_host;           // MySQL host (usually 'localhost')
var $db_username;       // MySQL username
var $db_password;       // MySQL password
var $db_name;			// MySQL database name
var $db_connection;		// MySQL database connection variable
var $db_select;			// MySQL database selection variable

function connect () {

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

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

} // END db_connect

function select () {

	$this -> db_select = mysql_select_db ($this -> database, $this -> db_connection);

		// Test the MySQL selection
		if (!$this -> db_select) { return false; } else { return true; }

}

}

// Class db_create

class db_create extends db_connect {

var $error;

// MySQL config
var $database_name;          // MySQL database name

function check_Existence () {

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

	if ($result){

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

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

		}

		if (!in_array($this -> database_name, $database_list)) {

			return true;

		}

	}
}

function create_Database () {

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

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

} // create_Database

function create () {

	if ($this -> check_Existence ()){

		if ($this -> create_Database ()){

			$error = "Database Created sucessfully.";

		} else {

			$error = "Database Could not be created.";

		}

	} else {

		$error = "The database you want to create already exists.";

	}

	return $error;

}

}

?>

 

And here is where I actually want to use my objects:

 

db_create_test.php

 

<?php

include ('classes/database_classes.php');

	  $db_connect = new db_connect ();
	  $db_connect -> db_host = 'localhost';
	  $db_connect -> db_username = 'root';
	  $db_connect -> db_password = '';
	  $db_connect -> connect();

	  $db_create = new db_create ();     // Call the class
	  $db_create -> database_name = 'Pastulio';        // Set the MySQL to be created database name
$result = $db_create -> create();

echo $result;

?>

 

Now the error I'm getting back is

 

Warning: mysql_list_dbs(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\classes\database_classes.php on line 47

 

This would indicate that the $this ->db_connection isn't being properly declared in my extended class

 

this is line 47:

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

 

Any further help would be great thanks

Link to comment
https://forums.phpfreaks.com/topic/57174-inheritance-need-help/#findComment-283078
Share on other sites

<?php

include ('classes/database_classes.php');

	  $db_connect = new db_connect ();
	  $db_connect -> db_host = 'localhost';
	  $db_connect -> db_username = 'root';
	  $db_connect -> db_password = '';
	  $db_connect -> connect();

	  $db_create = new db_create ();     // Call the class
	  $db_create -> database_name = 'Pastulio';        // Set the MySQL to be created database name
$result = $db_create -> create();

echo $result;

?>

 

$db_connect and $db_create are two ENTIRELY different instances.... For example if you were to set a variable in $db_connect, it would not be set in $db_create even though it extends it, because it is not extending the existing instance, it is creating a new instance of the db_create class which extends the other class....  The correct way to do this would be (well, I didn't look through your class definitions, but this should work ;p):

 

<?php

include ('classes/database_classes.php');

	  $db_create = new db_create ();     // Call the class
	  $db_create->db_host = 'localhost';
	  $db_create->db_username = 'root';
	  $db_create->db_password = '';
	  $db_create->connect();
	  $db_create -> database_name = 'Pastulio';        // Set the MySQL to be created database name
	  $result = $db_create -> create();

	  echo $result;

?>

 

db_create extends the db_connect class, meaning it has access to all of db_connects functions ;p.

 

I hope this made sense, but I'm not that great at explaining things -- especially when they're OOP related. ;p

Link to comment
https://forums.phpfreaks.com/topic/57174-inheritance-need-help/#findComment-285428
Share on other sites

That's actually the use of OOP under some languages (well, one of the uses).

 

For example, I've seen a gameserver coded in Java (yeah, Java... I know that's a bad choice so don't tell me).  Everytime someone connects to the server it creates a new instance of a class for them, and then this class extends other classes and so on.

 

And as for compatibility, if the code you posted works fine, then the code I posted should work fine as well.

 

(Well, if you thought your's would work fine, and I understood correctly what you were thinking it would do.)

Link to comment
https://forums.phpfreaks.com/topic/57174-inheritance-need-help/#findComment-286883
Share on other sites

448191 I'm not sure what you mean by that.... Do you mean, don't use classes for tasks in the sense that like, don't make a class to do one single thing, then make another class to do another and so on, but instead group similar actions under classes?

 

Also, I'm not sure who your suggestion was aimed at >.<.

Link to comment
https://forums.phpfreaks.com/topic/57174-inheritance-need-help/#findComment-288370
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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