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
https://forums.phpfreaks.com/topic/42080-using-class-in-php/
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
https://forums.phpfreaks.com/topic/42080-using-class-in-php/#findComment-204108
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
https://forums.phpfreaks.com/topic/42080-using-class-in-php/#findComment-204123
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.