ram4nd Posted January 10, 2009 Share Posted January 10, 2009 How can I use 1 mysql connection class in other class? Sql connection class: <?php class mysql { var $db_host, $db_name, $db_user, $db_password, $connect, $select, $query; function doConnect() { require_once('../database_settings.php'); $this->connect = mysql_connect($this->db_host, $this->db_user, $this->db_password); $this->select = mysql_select_db($database_name,$connection); if($this->connect) return true; else return false; } function doQuery($query) { $this->doConnect(); if(mysql_query($query, $this->connect)) { $this->disconnect(); return true; } else { $this->disconnect(); return false; } } function doDisconnect() { mysql_close($this->connection); } } ?> Quote Link to comment Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 require_once('../database_settings.php'); ok don't do that lol when you call the class $mysql = new mysql(); $mysql->user = 'user'; $mysql->host = 'host'; $mysql->pass = 'pass'; $mysql->doConnect(); or just do like $mysql = new mysql($host,$user,$pass); and in the mysql class do function __construct($host,$ussr,$pass) { $this->host = $host; $this->pass = $pass; $this->user = $user; } Quote Link to comment Share on other sites More sharing options...
ram4nd Posted January 10, 2009 Author Share Posted January 10, 2009 require_once('../database_settings.php') This code is in that: <?php $this->db_host = "localhost"; $this->db_name = "****"; $this->db_user = "****"; $this->db_password = "*******"; ?> But still how can I use my sql connection class in some other class. Is there something to do with parent and child classes? Quote Link to comment Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 you could pass it in via constructor $mysql = new mysql(); $e = new otherClass($mysql); then that object is available inside the class if you do like $this->mysql = $mysql; then... $this->mysql->query(); Quote Link to comment Share on other sites More sharing options...
ram4nd Posted January 10, 2009 Author Share Posted January 10, 2009 Do I need to make var $mysql, in the beginning of the "otherClass" class? Quote Link to comment Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 nope not really, aslong as you set it with $this->varname anywherte in the script, its set to the class' scope, rather than local scope Quote Link to comment Share on other sites More sharing options...
ram4nd Posted January 10, 2009 Author Share Posted January 10, 2009 Hmm, I am not getting it to work: <?php require_once('mysql_class.php'); class box { var $link_id; function name($link_id) { if(is_numeric($link_id)) { $mysql = new mysql(); $query = 'SELECT name FROM links WHERE id = ' + $link_id; $return = $mysql->doQuery($query); echo $return; } } } $box = new box($mysql); echo $box->name(1); ?> Quote Link to comment Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 yes because inside your box class you need function __construct($mysql) { $this->mysql = $mysql; } Quote Link to comment Share on other sites More sharing options...
9three Posted January 10, 2009 Share Posted January 10, 2009 I would leave any querying outside your classes. I wold also stop using var because it was deprecated in php5. Quote Link to comment Share on other sites More sharing options...
ram4nd Posted January 10, 2009 Author Share Posted January 10, 2009 Ok I added it, still nothing. There is 1 value in my db. Maybe the query is wrong?. I also repared my conection class: <?php class mysql { var $db_host, $db_name, $db_user, $db_password, $connect, $select, $query, $result; function doConnect() { require_once('../database_settings.php'); $this->connect = mysql_connect($this->db_host, $this->db_user, $this->db_password); $this->select = mysql_select_db($database_name, $this->connect); if($this->connect) return true; else return false; } function doQuery($query) { $this->doConnect(); $this->result = mysql_query($query); return $this->result; } function doDisconnect() { mysql_close($this->connect); } } ?> But that is not the reason why it doesn't work, 9three. Quote Link to comment Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 i still seriously think you can't require or include inside of a class =\ lol Quote Link to comment Share on other sites More sharing options...
ram4nd Posted January 10, 2009 Author Share Posted January 10, 2009 Well then do a little class and test it, you wont belive your eyes. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted January 10, 2009 Share Posted January 10, 2009 The code in here makes me want to cry... Okay, first, why are you using PHP 4? PHP 4 is old, and not even officially supported by Zend any more, to say nothing of its crappy OOP 'features.' If you're trying to use objects, you're much better off using PHP 5. Second, even if your db connection info is located in another file (which is good), you don't want to include it directly into your class file. It's a sloppy technique that leads to coupling, which is a very bad thing in OOP. A better designed class would be: class mysql { private $name; private $user; private $password; private $tableName; private $dbc; public function __connect($name, $user, $password) { $this->dbc = mysql_connect($name, $user, $password); if($this->dbc) { $this->name = $name; $this->user = $user; $this->password = $password; } else { die("There was an error connecting to the database."); } } public function selectTable($tableName) { if($this->dbc) { $this->tableName = mysql_select_db($tableName, $this->dbc); } else { die("Could not connect to the database."); } } public function query($query) { $result = mysql_query($query); //notice it's NOT $this->result... return $result; } public function fetch($result) { if(is_resource($result)) { $fetchedRow = mysql_fetch_assoc($result); return $fetchedRow; } else { die("Bad query result."); } } } In your client code, you'd use the class like so: require_once('../database_settings.php'); $mysql = new mysql($db_host, $db_user, $db_password); //Note NO $this-> $mysql->selectTable($tableName); $query = "SELECT * FROM users"; $result = $mysql->query($query); while($row = $mysql->fetch($result)) { /* do stuff */ } Quote Link to comment Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 public function __connect($name, $user, $password) I think you mean __construct however I know there are others like __destruct and __toString but I know __construct is usually the constructor Quote Link to comment Share on other sites More sharing options...
ram4nd Posted January 10, 2009 Author Share Posted January 10, 2009 All i had to do was to change my sql class code function like this: function doQuery($query) { $this->doConnect(); $result = mysql_fetch_row(mysql_query($query, $this->connect)); return $result[0]; } I am useing PHP 5. Ok basically I need a good link from wher i could learn about php classes. Because just when I thought that I am getting that classes thing then this showed up. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted January 10, 2009 Share Posted January 10, 2009 public function __connect($name, $user, $password) I think you mean __construct however I know there are others like __destruct and __toString but I know __construct is usually the constructor Yup. That's what I get for rushing. The class should work fine, though, if __connect is changed to __construct. 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.