Page 1 of 2

using class

Posted: Thu Nov 24, 2005 2:22 am
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

Class code for DB

Posted: Thu Nov 24, 2005 5:14 am
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

Re: Class code for DB

Posted: Mon Nov 28, 2005 10:50 pm
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

Posted: Tue Nov 29, 2005 1:52 am
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:

Posted: Tue Nov 29, 2005 2:23 am
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:

Posted: Tue Nov 29, 2005 2:51 am
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... :?:

Posted: Tue Nov 29, 2005 2:54 am
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... :?:

Posted: Tue Nov 29, 2005 3:06 am
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... :?:

Posted: Tue Nov 29, 2005 3:14 am
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... :?:

Posted: Tue Nov 29, 2005 3:30 am
by n00b Saibot
try require instead of include...

Posted: Tue Nov 29, 2005 3:38 am
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...

Posted: Tue Nov 29, 2005 4:01 am
by n00b Saibot
OK post both files here... I can't seem find a reason other than PHP not running...

Posted: Tue Nov 29, 2005 4:02 am
by Jenk
post your code please.

I'm willing to bet you haven't got <?php ?> tags surrounding the class :D

Posted: Tue Nov 29, 2005 4:09 am
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

Posted: Tue Nov 29, 2005 4:13 am
by n00b Saibot
aha... just as Jenk said (Good work ,Jenk ;))

add <?php ?> tags in mysql.php