using class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jaylin
Forum Commoner
Posts: 68
Joined: Fri Nov 18, 2005 4:44 am

using class

Post by jaylin »

hi all

in my website, all pages need to access mysql. so, i think database connection open statements are better to be put in a class. but, i m a very new user and i dun know how to do that. Is there any expert can point me out?

regards,
Jay
User avatar
anoopabose
Forum Newbie
Posts: 2
Joined: Thu Nov 24, 2005 5:02 am
Location: India ,Kerala

Class code for DB

Post by anoopabose »

twigletmac | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

if you are using php 4

Code: Select all

class db_mysql
{
	var $database;
	var $hostname;
	var $username;
	var $password;
	var $link;

        function db_mysql($hostname, $username, $password, $database)
	{
		$this->hostname = $hostname;
		$this->username = $username;
		$this->password = $password;
		$this->database = $database;
	}

	function connect()
	{	
		if(!$this->link = @mysql_connect($this->hostname,$this->username,$this->password) )
		{
			echo "Cannot Connect to host: ".$this->hostname;
			exit;
			return false;
		}
		else
		{
			return true;
		}
	}

	function select_db()
	{
		if(!@mysql_select_db($this->database, $this->link) )
		{
			echo "Cannot select database: ".$this->database;
			exit;
			return false;
		}
		else
		{
			return true;
		}
	}
	
}

For accessing this class
First create an object of the class

                                               $dbhost  = 'hostname';
			$dbuname = 'root';
			$dbpass  = 'root';
			$dbname  = 'dbName';
			
			$db = new db_mysql($dbhost,$dbuname,$dbpass,$dbname);
			$db->connect(); // calling mehods
			$db->select_db(); // calling mehods
Hopes this will work
jaylin
Forum Commoner
Posts: 68
Joined: Fri Nov 18, 2005 4:44 am

Re: Class code for DB

Post by jaylin »

plz tell me where i put the code of the class. is it necessary to save in another extension rather than php?
anoopabose wrote:twigletmac | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

if you are using php 4

Code: Select all

class db_mysql
{
	var $database;
	var $hostname;
	var $username;
	var $password;
	var $link;

        function db_mysql($hostname, $username, $password, $database)
	{
		$this->hostname = $hostname;
		$this->username = $username;
		$this->password = $password;
		$this->database = $database;
	}

	function connect()
	{	
		if(!$this->link = @mysql_connect($this->hostname,$this->username,$this->password) )
		{
			echo "Cannot Connect to host: ".$this->hostname;
			exit;
			return false;
		}
		else
		{
			return true;
		}
	}

	function select_db()
	{
		if(!@mysql_select_db($this->database, $this->link) )
		{
			echo "Cannot select database: ".$this->database;
			exit;
			return false;
		}
		else
		{
			return true;
		}
	}
	
}

For accessing this class
First create an object of the class

                                               $dbhost  = 'hostname';
			$dbuname = 'root';
			$dbpass  = 'root';
			$dbname  = 'dbName';
			
			$db = new db_mysql($dbhost,$dbuname,$dbpass,$dbname);
			$db->connect(); // calling mehods
			$db->select_db(); // calling mehods
Hopes this will work
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

save as {any-filename}.php then include it wherever required...

anoopabose: you should definitely consider adding more functionality to your class since only thing it does now is connect and select a db :wink:
jaylin
Forum Commoner
Posts: 68
Joined: Fri Nov 18, 2005 4:44 am

Post by jaylin »

sorry, i dun understand.

i dun know how to call the function in the class.

for example, i have a file called myclass.php In this file, i have a class call my_first_class. In the class i have a function called my_function.

Now, i have another php file. how can i access the function of myclass.php

regards,
n00b Saibot wrote:save as {any-filename}.php then include it wherever required...

anoopabose: you should definitely consider adding more functionality to your class since only thing it does now is connect and select a db :wink:
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

taking you example the sample code will be

Code: Select all

<?php
//include file in this script...
include "myfile.php";

//create an object of our class...
$obj = new my_first_class();

//call the function...
$obj->my_function();

?>
any more doubts... :?:
jaylin
Forum Commoner
Posts: 68
Joined: Fri Nov 18, 2005 4:44 am

Post by jaylin »

when i use include

Code: Select all

"myfile.php";
, all the words of the myfile.php are displayed. how can i solve it?

n00b Saibot wrote:taking you example the sample code will be

Code: Select all

<?php
//include file in this script...
include "myfile.php";

//create an object of our class...
$obj = new my_first_class();

//call the function...
$obj->my_function();

?>
any more doubts... :?:
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

jaylin wrote:when i use include "myfile.php"; all the words of the myfile.php are displayed. how can i solve it?
What??? are you sure you running that from server with PHP installed... :?:
jaylin
Forum Commoner
Posts: 68
Joined: Fri Nov 18, 2005 4:44 am

Post by jaylin »

if i write the code in the page rather than in the class on other page, it works fine. but, when i put include "myclass.php" in the page, all the words in the class are displayed in the page.

plz help me
n00b Saibot wrote:
jaylin wrote:when i use include "myfile.php"; all the words of the myfile.php are displayed. how can i solve it?
What??? are you sure you running that from server with PHP installed... :?:
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

try require instead of include...
jaylin
Forum Commoner
Posts: 68
Joined: Fri Nov 18, 2005 4:44 am

Post by jaylin »

i tried, still display the terrible code of the class ... :x :x :x :x :x :x :x :x :x :x :x :x :x :x :x :x :x :x :x :x :x

help me!!!
n00b Saibot wrote:try require instead of include...
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

OK post both files here... I can't seem find a reason other than PHP not running...
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

post your code please.

I'm willing to bet you haven't got <?php ?> tags surrounding the class :D
jaylin
Forum Commoner
Posts: 68
Joined: Fri Nov 18, 2005 4:44 am

Post by jaylin »

mysql.php

Code: Select all

class db_sql
{
	var $link;
	function connect()
	{
		$link = mysql_connect("localhost","myuser","mypassword");
		if ($link)
		{
			$db= mysql_selectdb("mydb");
			if ($db)
				echo 'great man';
		}
			
	}
}
test.php

Code: Select all

<?php
//include file in this script...
require "mysql.php";

//create an object of our class...
$obj = new db_sql();

//call the function...
$obj->connect();

?>
plz
Jenk wrote:post your code please.

I'm willing to bet you haven't got <?php ?> tags surrounding the class :D
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

aha... just as Jenk said (Good work ,Jenk ;))

add <?php ?> tags in mysql.php
Post Reply