Pastulio Posted June 26, 2007 Share Posted June 26, 2007 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 Quote Link to comment Share on other sites More sharing options...
emehrkay Posted June 26, 2007 Share Posted June 26, 2007 you could extend connect_db class create_databse extends connect_db{ function create_database(){ if($this->db_connect()){ $this->db_select(); } ... } Quote Link to comment Share on other sites More sharing options...
Pastulio Posted June 26, 2007 Author Share Posted June 26, 2007 Cool thanks man Quote Link to comment Share on other sites More sharing options...
Pastulio Posted June 26, 2007 Author Share Posted June 26, 2007 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 Quote Link to comment Share on other sites More sharing options...
effigy Posted June 26, 2007 Share Posted June 26, 2007 Are you aware that you can extend the existing mysqli class (if available)? Quote Link to comment Share on other sites More sharing options...
Pastulio Posted June 26, 2007 Author Share Posted June 26, 2007 I am not, but I can't figure out what I'm doing wrong here =( Quote Link to comment Share on other sites More sharing options...
effigy Posted June 26, 2007 Share Posted June 26, 2007 How are you storing $this->db_connection? Quote Link to comment Share on other sites More sharing options...
Pastulio Posted June 26, 2007 Author Share Posted June 26, 2007 Well it's just a variable for the other class as you can see in my code Quote Link to comment Share on other sites More sharing options...
emehrkay Posted June 27, 2007 Share Posted June 27, 2007 you never run the connect() method. doesnt that method define $this -> db_connection ? Quote Link to comment Share on other sites More sharing options...
Pastulio Posted June 28, 2007 Author Share Posted June 28, 2007 Yes but I was not aware that I had to run it there and then because my page will only need to connect once generally, so is it absolutely required to run one class simultaneously with the other when using Inheritance? Quote Link to comment Share on other sites More sharing options...
corbin Posted June 29, 2007 Share Posted June 29, 2007 <?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 Quote Link to comment Share on other sites More sharing options...
Pastulio Posted June 29, 2007 Author Share Posted June 29, 2007 It might not be 100% compatible with my code, but it makes sense to me, so thanks a lot . Didn't know they had to be the same "instance". Quote Link to comment Share on other sites More sharing options...
corbin Posted June 30, 2007 Share Posted June 30, 2007 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.) Quote Link to comment Share on other sites More sharing options...
448191 Posted July 1, 2007 Share Posted July 1, 2007 Just a tip: have objects represent objects, not tasks. Quote Link to comment Share on other sites More sharing options...
corbin Posted July 2, 2007 Share Posted July 2, 2007 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 >.<. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.