Jump to content

Using class in php


jeeva

Recommended Posts

his frnds,

 

i am not good in php class,so i need ur help ..

i have one class as database wrapper and i need to use this class from another class.i hear that i need to create one object to call that class.but i have used like that  but its not working i dont know whether its right or wrong.

 

Here my code

class db
{

var $dsn="dsn"; //dsn
var $uname="";
var $password="";
var $con=NULL;
//db connect
function DbConnect()
{
	$this->con=odbc_connect($this->dsn,$this->uname,$this->password) or die(odbc_errormsg());
	return $this->con;
}
//function to select
function Select($tableName,$condition)
{
	$sql="SELECT * from $tableName WHERE 1=1 $condition";
	$this->select=odbc_exec($this->con,$sql);
	return $this->select;
}
//get the values from result
function Getdata($resourceid,$fieldName)
{
	$result=odbc_result($resourceid,$fieldName);
	return $result;
}

}


class GlobalVars
{

$db=new db();
$dbcon=$db->DbConnect();

function UserDetails($fieldName)
{


	$tableName="Employee";
	$condition="and Windowloginname='$loginuser'";

	$select=$this->db->Select($tableName,$condition);
	$value=$this->db->GetData($select,fieldName);
	return $value;
}

}

 

can u please tell me wt i have to do?

 

 

 

jeeva

Link to comment
Share on other sites

Constructors are key here. They are what get called when the class is instantiated. Although this is a poorly designed class here is what it should be.

<?php

class db
{

var $dsn="dsn"; //dsn
var $uname="";
var $password="";
var $con=NULL;
//db connect

// constructor
function db() {
                $this->dsn="dsn"; //dsn  -- for PHP 4 as the variable declaration does not work.
	$this->DbConnect();
}

function DbConnect()
{
	$this->con=odbc_connect($this->dsn,$this->uname,$this->password) or die(odbc_errormsg());
	return $this->con;
}
//function to select
function Select($tableName,$condition)
{
	$sql="SELECT * from $tableName WHERE 1=1 $condition";
	$this->select=odbc_exec($this->con,$sql);
	return $this->select;
}
}


class GlobalVars
{
// constructor
function GlobalVars() {

	$db=new db();
}

function UserDetails($fieldName)
{


	$tableName="Employee";
	$condition="and Windowloginname='$loginuser'";

	$select=$this->db->Select($tableName,$condition);
	$value=$this->db->GetData($select,fieldName);
	return $value;
}
//get the values from result
function Getdata($resourceid,$fieldName)
{
	$result=odbc_result($resourceid,$fieldName);
	return $result;
}

}
?>

 

If you would like to take a look at my db class just let me know I will be more than happy to show it off.

 

--FrosT

Link to comment
Share on other sites

changed: $this->db=new db();  my bad. Forgot about that portion.

 

<?php

class db
{

var $dsn="dsn"; //dsn
var $uname="";
var $password="";
var $con=NULL;
//db connect

// constructor
function db() {
                $this->dsn="dsn"; //dsn  -- for PHP 4 as the variable declaration does not work.
	$this->DbConnect();
}

function DbConnect()
{
	$this->con=odbc_connect($this->dsn,$this->uname,$this->password) or die(odbc_errormsg());
	return $this->con;
}
//function to select
function Select($tableName,$condition)
{
	$sql="SELECT * from $tableName WHERE 1=1 $condition";
	$this->select=odbc_exec($this->con,$sql);
	return $this->select;
}
}


class GlobalVars
{
// constructor
function GlobalVars() {

	$this->db=new db();
}

function UserDetails($fieldName)
{


	$tableName="Employee";
	$condition="and Windowloginname='$loginuser'";

	$select=$this->db->Select($tableName,$condition);
	$value=$this->db->GetData($select,fieldName);
	return $value;
}
//get the values from result
function Getdata($resourceid,$fieldName)
{
	$result=odbc_result($resourceid,$fieldName);
	return $result;
}

}
?>

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.