c_shelswell Posted March 25, 2009 Share Posted March 25, 2009 Hi I'm trying to learn a little about php classes and thought a database class might be a good idea as it's something most of my sites would use. I found a bit of code online and i'm trying to adapt it to work for me. Problem is I can't seem to connect to the database though I know my username and password is correct. I'm not too sure what i've done with the __construct bit does that automatically start? Also when i call the db class do i have to start with the connect function id $db->connect(); or can i just jump straight to $db->selectFromDB(); It would be good if i didn't have to connect every time, i'm trying to reduce the amount of code i have to write. My code's below if you could help that'd be great <?php include ("constants.php"); class db { private $dbUser; private $dbName; private $dbPass; private $dbServer; private $connection; private $result; public function __contsruct($dbUser=DB_USER, $dbName=DB_NAME, $dbPass=DB_PASS, $dbServer=DB_SERVER) { $this->dbUser = $dbUser; $this->dbName = $dbName; $this->dbPass = $dbPass; $this->dbServer = $dbServer; $this->connect(); } private function connect() { $this->connection = mysql_pconnect($this->dbServer, $this->dbUser, $this->dbPass) or die (mysql_error()); mysql_select_db($this->dbName, $this->connection)or die(mysql_error()); } private function disconnect() { mysql_close($this->connection); } public function selectFromDB($data) { extract($data); $sql = 'SELECT '.$fields.' FROM '.$table; if(!empty($where)) { $sql .= ' WHERE '.$where; } if(!empty($groupBy)) { $sql .= ' GROUP BY '.$group_by; } if(!empty($orderBy)) { $sql .= ' ORDER BY '.$order_by; } if(!empty($limit)) { $sql .= ' LIMIT '.$limit; } $this->result = $this->query($sql); if($this->result) { $rows = $this->fetchRows(); } return $rows; } private function fetchRows() { $rows = array(); if($this->result) { while($row = @mysql_fetch_object($this->result)) { $rows[] = $row; } } return $rows; } private function query($sql) { $this->result = mysql_query($sql) or die (mysql_error()); return $this->fetchRows(); } } ?> I'm calling it like: $db = new db(); $data = array('fields'=>'username', 'table'=>'registered_users', 'where'=>'username="'.$_POST['username'].'"'); $db->selectFromDB($data); Many thanks Link to comment https://forums.phpfreaks.com/topic/151094-solved-trying-to-make-a-database-class-but-cant-connect/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 25, 2009 Share Posted March 25, 2009 What error or other symptom are you getting that leads you to believe it is not connecting to the database server? For the rest of your question - Constructor void __construct ([ mixed $args [, $... ]] ) PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used. Link to comment https://forums.phpfreaks.com/topic/151094-solved-trying-to-make-a-database-class-but-cant-connect/#findComment-793746 Share on other sites More sharing options...
c_shelswell Posted March 25, 2009 Author Share Posted March 25, 2009 Cheers PFM i'm getting Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\a1\require\database_class.php on line 121 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\a1\require\database_class.php on line 121 Access denied for user 'ODBC'@'localhost' (using password: NO) if i try and just connect from another page with the user and pass i'm providing the class it works fine. So I'm pretty sure it's something to do with my class. Cheers Link to comment https://forums.phpfreaks.com/topic/151094-solved-trying-to-make-a-database-class-but-cant-connect/#findComment-793750 Share on other sites More sharing options...
PFMaBiSmAd Posted March 25, 2009 Share Posted March 25, 2009 You have a spelling error in __contsruct that prevents the constructor being being seen or called. Link to comment https://forums.phpfreaks.com/topic/151094-solved-trying-to-make-a-database-class-but-cant-connect/#findComment-793761 Share on other sites More sharing options...
c_shelswell Posted March 25, 2009 Author Share Posted March 25, 2009 Brilliant - thanks it's always the really simple things i've been looking at the page of code for ages and just couldn't see that!! Cheers Link to comment https://forums.phpfreaks.com/topic/151094-solved-trying-to-make-a-database-class-but-cant-connect/#findComment-793764 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.