using class
Moderator: General Moderators
using class
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
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
- anoopabose
- Forum Newbie
- Posts: 2
- Joined: Thu Nov 24, 2005 5:02 am
- Location: India ,Kerala
Class code for DB
twigletmac | Please use
Hopes this will work
Code: Select all
andCode: 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 4Code: 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 mehodsRe: Class code for DB
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 useCode: Select all
andCode: 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 4Hopes this will workCode: 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
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
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,
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
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
taking you example the sample code will be
any more doubts... 
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();
?>when i use include, all the words of the myfile.php are displayed. how can i solve it?
Code: Select all
"myfile.php";n00b Saibot wrote:taking you example the sample code will beany more doubts...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(); ?>
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
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
plz help me
n00b Saibot wrote:What??? are you sure you running that from server with PHP installed...jaylin wrote:when i use include "myfile.php"; all the words of the myfile.php are displayed. how can i solve it?
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
mysql.php
test.php
plz
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';
}
}
}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();
?>Jenk wrote:post your code please.
I'm willing to bet you haven't got <?php ?> tags surrounding the class
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact: