Jump to content

Classes


ram4nd

Recommended Posts

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);
}
}
?>

Link to comment
Share on other sites

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;

}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 */
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

   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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.