I have to classes - database and dredgy. An example of a function in database would be: /*The select function is used for selecting and outputting a value from the database*/ function select($column,$table,$arguments=NULL){ //Compiles the arguments in the function to a valid MySQL Query. $query = "SELECT ".$column." FROM ".$table." ".$arguments.""; //Execute query, or display an error if it fails. mysql_query($query) or die(mysql_error()); return true; } PHP: Now I want to call that function in a function in my other class (dredgy). How would I do that? Thanks, BP
If the method isn't static, you should create an instance. <?php $yourClass = new ExampleClass(); $yourClass->the_method(); ?> PHP: If it's a static method, simply do: <?php ExampleClass::the_method(); ?> PHP:
Well, :: seems to work, but I think I might be having an error with the combinations of functions I am using: Function 1 (select from database): function select($column,$table,$arguments=NULL){ //Compiles the arguments in the function to a valid MySQL Query. $query = "SELECT ".$column." FROM ".$table." ".$arguments.""; //Execute query, or display an error if it fails. return mysql_query($query) or die(mysql_error()); } Code (markup): Function 2 (fetch array): function fetch_array($query){ //Fetches the array of the query. return mysql_fetch_array($query); } Code (markup): Function 3: function escape($variable){ //Sanitize the variable. return mysql_real_escape_string($variable); } Code (markup): Function 4 (retrieve a setting from the database) - stored in another class: /*The setting function retrieves a part of a setting from a database.*/ function setting($setting_name,$part,$divider=" - "){ //To save typing, if $part is equal to "name", the script readjusts it to the value in the database, which is settingname. if($part=="name"){$part="settingname";} //The query to select all information from the settings table $query = database::select("*","settings","WHERE settingname = '".database::escape($setting_name)."'"); //Retrieve the settings and set as variables while($settings = database::fetch_array($query)) { //If $part is equal to all, output all the parts of the setting - The Setting Name, The Setting Label and The Setting Value. if($part=="all") { $output = $settings['settingname'].$divider.$settings['label'].$divider.$settings['value']; } //If $part does not equal all, just select the speicfied part else{ $output = $settings[$part]; } } //Returns the output return $output; } Code (markup): And my error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\cms\classes\class_mysql.php on line 41 Line 41 is: return mysql_fetch_array($query); Code (markup): of the fetch_array function. That should be returning a valid query... Thanks, BP EDIT: I even used a mysql_query() instead of the select method, and it still had the same error
First step is to verify that you are connecting to MySQL successfully. Second step would be to verify that you are successfully selecting a valid database. If those are both good, do a var_dump on $query right after you set it to see what it is actually being set to.
The var_dum showed it as bool(true), so that can't be right. Keep the suggestions coming while I try to figure this one out...
Ok, I modified this (just changed the variable name): //The query to select all information from the settings table $select_query = database::select("*","settings","WHERE settingname = '".database::escape($setting_name)."'"); Code (markup): And that is bool(true), and is what is being passed on to the fetch_array....
I'm about to head to bed and so my thoughts might not be totally clear, but "true" means that it did a query like update, delete, etc. I guess the next step is to do the whole thing outside of your class, step by step with the normal functions. If that works, replace them one at a time with the class methods and see what happens. Another thing you could check is if mysql_affected_rows() returns something after you run the SELECT query.
Well, I threw in a die() to catch the query being run, as well as compare it to the query that worked (outside of the class) - I got: SELECT * FROM settings WHERE settingname = 'sitename' SELECT * FROM settings WHERE settingname = 'sitename' Code (markup): So they are exactly the same, it should be something with fetch_array then, or just with the class in general. Because I have used all the functions successfully before, but they just seem to not work when being called from the other class, so there has to be a way to include them properly.
I am 99% sure that this is a problem with my select function: function select($column,$table,$arguments=NULL){ //Compiles the arguments in the function to a valid MySQL Query. $query = "SELECT ".$column." FROM ".$table." ".$arguments.""; //Execute query, or display an error if it fails. return mysql_query($query) or die(mysql_error()); } Code (markup): If for some reason it helps, here are the two classes in full: class_mysql.php: <?php /***************************************************************************************************** The Database Class is for interactions with the database. The idea behind this class is if users are using different database engines (MySQL, PostgreSQL,SQLite or something else), the functions in this class will act the same. For example, using mysql_connect() will not work on any other database engines, asides from MySQL. However, $db->connect() will work on what ever systems are included in Dredgy. This class is for: MySQL *****************************************************************************************************/ class database{ /*The connect function connects to - and selects - the correct Dredgy database.*/ function connect(){ //Globals all the database details found in settings.php. global $dbhost,$dbusername,$dbpassword,$dbname; //Connect to MySQL $connection = mysql_connect($dbhost,$dbusername,$dbpassword) or die(mysql_error()); //Select the database that Dredgy is installed on mysql_select_db($dbname,$connection) or die (mysql_error()); } /*The select function is used for selecting and outputting a value from the database*/ function select($column,$table,$arguments=NULL){ //Compiles the arguments in the function to a valid MySQL Query. $query = "SELECT ".$column." FROM ".$table." ".$arguments.""; //Execute query, or display an error if it fails. return mysql_query($query) or die(mysql_error()); } /*The fetch_array function should hopefullly explain itself. */ function fetch_array($query){ //Fetches the array of the query. return mysql_fetch_array($query); } /*The fetch_field function should hopefullly explain itself. */ function fetch_field($query,$offset=0){ //Fetch the field from the database. mysql_fetch_field($query,$offset); return true; } /*The Insert function inserts a new row into the database, with predefined column and value variables*/ function insert($table,$columns,$values){ //The start of the actual Query $query = "INSERT INTO ".$table." ("; //If you are oly inserting ONE value into ONE column, this will finish of the query. if(!is_array($columns) AND !is_array($values)){ //The end of the actual query. $query .= $columns.") VALUES ('".$values."')"; } //However, if you are inserting MULTIPLE values into MULTIPLE columns, some work has to be done. else{ //For each individual value in the 'columns' array. foreach($columns as $column){ //Append the column to the query. $query .= $column." "; } //The end of the first half of the query, and beginning of the second half. $query .= ") VALUES ("; //For each individual value in the 'valuess' array. foreach ($values as $value){ //Apend the value to the query. $query .= $value." "; } //The end of the actual query. $query .= " )"; } mysql_query($query) or die(mysql_error()); return true; } /*The update function allows to replace an existing value in a database with a new one*/ function update($table,$column,$value,$argument){ //Compile the query from the arguments provided. $query = "UPDATE ".$table." SET ".$column." = '".$value."' ".$argument; //Run the query ot output an error. mysql_query($query) or die(mysql_error()); return true; } /*The escape function sanitizes a variable before inputting into the database*/ function escape($variable){ //Sanitize the variable. return mysql_real_escape_string($variable); } } ?> PHP: class_dredgy.php: <?php /********************************************************************************** The Dredgy Class is for easy and direct interaction and manipulation of the Dredgy CMS script. **********************************************************************************/ class dredgy{ /*The version output function outputs the version of Dredgy*/ function version(){ //Set the necessary script information - do not change "name" if you don't want to be sued. Do not change version or subversion number - some things may function incorrectly */ $script = array( "name" => "Dredgy CMS", "version" => "0", "subversion" => ".0 Lite[beta]" ); //Return the necessary details return $script["name"]." version ".$script["version"].$script["subversion"].""; } /*The New User Function creates a new user on Dredgy*/ function new_user($username,$password,$group){ //Adds some extra security to the password, making it almost impossible to crack. $password = sha1($password); $password = md5 ($password); $password = sha1($password); $password = base64_encode($password); $password = str_rot13($password); //An array which stores the columns to insert the new user into. $cols = array('username,','password,','usergroup'); //An array which stores the values for the new user. $values = array("'".database::escape($username)."'" ,"'".database::escape($password)."'","'".database::escape($group)."'"); //Insert the user into the database. database::insert('user',$cols,$values); return true; } /*The setting function retrieves a part of a setting from a database.*/ function setting($setting_name,$part,$divider=" - "){ //To save typing, if $part is equal to "name", the script readjusts it to the value in the database, which is settingname. if($part=="name"){$part="settingname";} //The query to select all information from the settings table $select_query = database::select("*","settings","WHERE settingname = '".database::escape($setting_name)."'"); //Retrieve the settings and set as variables while($settings = mysql_fetch_array($select_query)) { //If $part is equal to all, output all the parts of the setting - The Setting Name, The Setting Label and The Setting Value. if($part=="all") { $output = $settings['settingname'].$divider.$settings['label'].$divider.$settings['value']; } //If $part does not equal all, just select the speicfied part else{ $output = $settings[$part]; } } //Returns the output return $output; } } ?> PHP: The function I want to use is (in dredgy class) is setting(). Thanks The error is: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\cms\classes\class_mysql.php on line 41 And I have double checked - the query being returned by the select function is the exact same one I use when using mysql_query() (which works btw). The query is: $query = $db->select("*","settings","WHERE settingname = 'sitename'"); while($name = $db->fetch_array($query)){ $output = $name['settingname']; } Code (markup): or $dredgy->setting("sitename","value");