Q Posted July 8, 2009 Share Posted July 8, 2009 Hi guys I'm working on a project at work that requires me to use a database connection in my files class. My files are: db.class.php file.class.php But with this code, the file.class.php file can not see the open SQL connection: include("class/db.class.php"); $db = new db("MSSQL"); $db->connect("server", "user", "password", "database"); include("class/file.class.php"); $files = new files(); The error returned looks like this: Notice: Undefined variable: db in D:\HSE2\class\file.class.php on line 61 Fatal error: Call to a member function query() on a non-object in D:\HSE2\class\file.class.php on line 61 Any suggestions? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/165143-solved-file-class-cant-access-database-class/ Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 Two methods: If you continue with your current approach, put the two $db calls inside of the files function, otherwise you could connect to the database using the two lines of code below in a file that is included in the script that calls the files method. I'd recommend the 2nd option because you would never have to repeat the process of connecting to the database. mysql_connect('localhost','db_user','db_pass'); @mysql_select_db(DB_DATABASE) or die("Unable to select database."); Quote Link to comment https://forums.phpfreaks.com/topic/165143-solved-file-class-cant-access-database-class/#findComment-870791 Share on other sites More sharing options...
Q Posted July 8, 2009 Author Share Posted July 8, 2009 Should that really be necessary? I mean, I've done this a ton of times before on linux boxes, just with the connection to the database in the constructor of my database class. Anyway, thanks for the reply! I'll have a look at it.. Edit: Also I can't do what you suggest, since i need some functions of the database class to run inside the file class.. Quote Link to comment https://forums.phpfreaks.com/topic/165143-solved-file-class-cant-access-database-class/#findComment-870794 Share on other sites More sharing options...
p2grace Posted July 8, 2009 Share Posted July 8, 2009 You can call the database from the constructor of the class if you want, that's simple enough. But using a configuration file that contains the database information is a major shortcut and time saver since it only needs to be required at the top of the script once, and doesn't need to be referred to again (because it's automatically inherited). Quote Link to comment https://forums.phpfreaks.com/topic/165143-solved-file-class-cant-access-database-class/#findComment-870799 Share on other sites More sharing options...
Q Posted July 8, 2009 Author Share Posted July 8, 2009 If I do so, i get an error saying the class can't be re-declared. - I need some other alternative.. I don't know if this means anything, but I'm on windows server. Quote Link to comment https://forums.phpfreaks.com/topic/165143-solved-file-class-cant-access-database-class/#findComment-870819 Share on other sites More sharing options...
trq Posted July 8, 2009 Share Posted July 8, 2009 I don't know if this means anything, but I'm on windows server. No, it doesn't mean anything. Put simply, your $db object does not exist within your files class. You can make it exist by passing it into the constructor or by setting up a new instance of the db object (again, within your constructor). Quote Link to comment https://forums.phpfreaks.com/topic/165143-solved-file-class-cant-access-database-class/#findComment-870834 Share on other sites More sharing options...
Q Posted July 8, 2009 Author Share Posted July 8, 2009 So you are saying I can do what I want just by passing an argument in my constructor? If I make a function in my DB class that returns $this; and then passes that function to my files.class.php in the constructor, it will replicate the DB class to use in the files.class.php? I solved this issue in another way though, a crappy way. I extended my files class to my db class Quote Link to comment https://forums.phpfreaks.com/topic/165143-solved-file-class-cant-access-database-class/#findComment-870847 Share on other sites More sharing options...
Q Posted July 9, 2009 Author Share Posted July 9, 2009 No body's got a solution for me? Quote Link to comment https://forums.phpfreaks.com/topic/165143-solved-file-class-cant-access-database-class/#findComment-871711 Share on other sites More sharing options...
eldorik Posted July 9, 2009 Share Posted July 9, 2009 Are you calling the variable $db inside the file class? Quote Link to comment https://forums.phpfreaks.com/topic/165143-solved-file-class-cant-access-database-class/#findComment-871720 Share on other sites More sharing options...
Q Posted July 9, 2009 Author Share Posted July 9, 2009 Yes Here's some code snippets: db.class.php <?php class db { /* Initial variables for the class ------------------------------------------------------------------*/ private $type, $database, $server, $user, $pass; /* Class constructor ------------------------------------------------------------------*/ public function __construct($type="MySQL", $server, $user, $pass, $database) { switch($type) { case "MySQL": $type = 1; break; case "MSSQL": $type = 2; break; } $this->type = $type; //Connect to database if ($this->type == 1) { $srv = mysql_connect($server, $user, $pass); mysql_select_db($database, $srv); } elseif ($this->type == 2) { $srv = mssql_connect($server, $user, $pass); mssql_select_db($database, $srv); } $this->database = $database; $this->server = $server; $this->user = $user; $this->pass = $pass; } /* Functions All functions of this file listed below ------------------------------------------------------------------*/ /* Connect Connect to database server and select database ------------------------------------------------------------------*/ public function connect($server, $user, $pass, $database) { if ($this->type == 1) { $srv = mysql_connect($server, $user, $pass); mysql_select_db($database, $srv); } elseif ($this->type == 2) { $srv = mssql_connect($server, $user, $pass); mssql_select_db($database, $srv); } $this->database = $database; $this->server = $server; $this->user = $user; $this->pass = $pass; } /* Close Close current database connection. ------------------------------------------------------------------*/ public function close() { if ($this->type == 1) { mysql_close(); } elseif ($this->type == 2) { mssql_close(); } } /* List tables Lists tables in database ------------------------------------------------------------------*/ public function listTables() { $res = ""; if ($this->type == 1) { $query = mysql_list_tables($this->database); } elseif ($this->type == 2) { $query = "This function is MySQL only!"; } return $query; $this->free_result($query); } /* Query Query to SQL Remember to check return for false! ------------------------------------------------------------------*/ public function query($sql, $debug=false) { if ($debug == true) { echo $sql . "<br /><br />"; } if ($this->type == 1) { return mysql_query($sql); } elseif ($this->type == 2) { return mssql_query($sql); } } /* Pull query Pull information from database ------------------------------------------------------------------*/ public function pull($sql, $debug=false) { //Define variable to prevent E_NOTICE if no results is returned. $resultArray[] = ""; if ($this->type == 1) { $query = $this->query($sql, $debug); $i=0; while ($r = mysql_fetch_object($query)) { foreach ($r AS $key => $value) { $resultArray[$i][$key]=$value; } $i++; } } elseif ($this->type == 2) { $query = $this->query($sql, $debug); $i=0; while ($r = mssql_fetch_object($query)) { foreach ($r AS $key => $value) { $resultArray[$i][$key]=$value; } $i++; } } return $resultArray; } /* Free result Free the result from server RAM ------------------------------------------------------------------*/ private function free_result($query) { if ($this->type == 1) { mysql_free_result($query); } elseif ($this->type == 2) { mssql_free_result($query); } } /* List databases Lists the avaliable databases on the server ------------------------------------------------------------------*/ public function listDbs() { if ($this->type == 1) { $server = mysql_connect($this->server, $this->user, $this->pass); $databases = mysql_list_dbs($server); $res = "<pre>"; while ($r = mysql_fetch_object($databases)) { $res .= $r->Database; } $res .= "</pre>"; $this->close(); } elseif ($this->type == 2) { $res = "Make MSSQL function first!"; } return $res; } /* Num rows Returns the number of rows given by the query ------------------------------------------------------------------*/ public function num_rows($sql, $debug=false) { if ($this->type == 1) { $query = $this->query($sql, $debug); return mysql_num_rows($query); } elseif ($this->type == 2) { $query = $this->query($sql, $debug); return mssql_num_rows($query); } } /* Escape string Escapes the query string ------------------------------------------------------------------*/ public function escape_string($string) { if ($this->type == 1) { return mysql_real_escape_string($string); } elseif ($this->type == 2) { $return = str_replace("'","''",$string); return $return; } } /* Insert id Get the insert id of the just inserted row ------------------------------------------------------------------*/ public function insert_id() { if ($this->type == 1) { return mysql_insert_id(); } elseif ($this->type == 2) { $id = false; $res = mssql_query("SELECT @@indentity AS id"); if ($row = mssql_fetch_row($res)) { $id = trim($row[0]); } mssql_free_result($res); return $id; } } /* Remake Returns $this to use the functions of this class in other classes. ------------------------------------------------------------------*/ public function remake() { return $this; } } ?> files.class.php (The relevant part) <?php class files { /* Initial variables for the class ------------------------------------------------------------------*/ /* Class constructor ------------------------------------------------------------------*/ public function __construct() { $db = new db("MSSQL", "server", "user", "password", "database"); } /* Functions All functions of this file listed below ------------------------------------------------------------------*/ /* Upload This function uploads a file to the server, and adds information to a database. 7 parameters are used; $file, $types, $location, $name, $table, $userId, $category. $file = File recieved from upload form (Form name) $types = array with allowed filetypes (can be all, to allow anything) $location = The decired location on the server. $name = The users name for the file. $table = The table in the database to hold the file information. $userId = ID of the user who uploaded the file. $category = Category for the file. (May be empty) ------------------------------------------------------------------*/ public function upload($file, $types, $location, $name, $table, $userId, $category=0, $update=false) { if ($_FILES[$file] > 0) { if ($_FILES[$file]["size"] <= 25165824) { //24 megabytes in bytes $fileExtension = explode("/", $_FILES[$file]["type"]); $fEx = explode(".", $_FILES[$file]["name"]); $fExCount = count($fEx); $fExtension = $fEx[$fExCount-1]; if ($types == "all" || @in_array($fileExtension[1], $types)) { $actualFile[0] = $_FILES[$file]["name"]; if ($fileExtension[1] == "quicktime") { $fileExtension[1] = "mov"; } elseif ($fileExtension[1] == "x-ms-wmv") { $fileExtension[1] = "wmv"; } elseif ($fileExtension[1] == "vnd.openxmlformats-officedocument.wordprocessingml.document") { $fileExtension[1] = "docx"; } elseif ($fileExtension[1] == "vnd.openxmlformats-officedocument.spreadsheetml.sheet") { $fileExtension[1] = "xlsx"; } $finalUploaded = $location . "/" . $actualFile[0]; $finalUploaded = ereg_replace(" ", "_", $finalUploaded); if(move_uploaded_file($_FILES[$file]["tmp_name"], $finalUploaded)) { $content = ""; if (!$update === false) { return $db->query("UPDATE " . $table . " SET fileDir = '" . $location . "', fileName = '" . $actualFile[0] . "', userId = '" . $userId . "', name = '" . $name . "', fileType = '" . $fExtension . "', category = '" . $category . "' WHERE id = '" . $update . "'", true); } else { return $db->query("INSERT INTO [" . $table . "] (fileDir, fileName, userId, name, fileType, datetime, content) VALUES ('" . $location . "','" . $finalUploaded . "','" . $userId . "','" . $name . "','" . $fExtension . "', '" . time() . "', '" . substr($content, 0, 7995) . "')"); return $db->insert_id(); } } else { $error = "UNKNOWN_ERROR"; return $error; } } else { $error = "WRONG_FILETYPE"; return $error; } } else { $error = "FILE_TOO_LARGE"; return $error; } } else { $error = "NO_INPUT_FILE"; return $error; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165143-solved-file-class-cant-access-database-class/#findComment-871726 Share on other sites More sharing options...
trq Posted July 9, 2009 Share Posted July 9, 2009 You need to create a property to store your db object within. eg; class files { private $db; public function __construct() { $this->db = new db("MSSQL", "server", "user", "password", "database"); } // more code } Then use $this->db to refer to your database object. Quote Link to comment https://forums.phpfreaks.com/topic/165143-solved-file-class-cant-access-database-class/#findComment-871817 Share on other sites More sharing options...
Q Posted July 9, 2009 Author Share Posted July 9, 2009 Aah.. Finally I understood it! - Thank you very very much! Quote Link to comment https://forums.phpfreaks.com/topic/165143-solved-file-class-cant-access-database-class/#findComment-871819 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.