/    Sign up×
Community /Pin to ProfileBookmark

php function question….

Hello guys!

I am trying to figure out how to connect to a database from a function that is within a class. There is a class that has a connection function defined and here is the code for it.

[code=php]
class TDatabase {

var $host;
var $user;
var $passwd;
var $db;
var $hLink;
var $isConnected; // boolean to check if the connection is active
var $isPConnected; // boolean to check if the connection is persistent or not when the connection is active

// the field $hDB has been killed because it has no role

// I would like to add a constructor with $host, $user, $passwd and $db as
// parameters and to not add set and get methods for these fields.
function TDatabase($host, $user, $passwd, $db) {
$this->host = $host;
$this->user = $user;
$this->passwd = $passwd;
$this->db = $db;
$this->isConnected = false;
}

function connect() {
// for non persistent connections the best choice in many case
if (!$this->isConnected) {
$this->hLink = @mysql_connect($this->host, $this->user, $this->passwd);
if (($this->hLink) && (@mysql_select_db($this->db, $this->hLink))) {
$this->isConnected = true; // the connection is active
$this->isPConnected = false; // this is a non persistent connection
return true; // the connection is successfull and the database is selected
}
else
return false; // an error has occured
}
else
return false; // already connected
}

function output() {
echo “Database info: “;
echo “hostname=$host, user=$user, passwd=$passwd,db_name=$dbn”;
if ($isConnected)
echo “,connected to server”;
if ($isPConnected)
echo “,persistent connection”;
echo “n”;
}

}
[/code]

No I use that within a file that I will include into another for my use. Now I tried calling up the function in another page such as:

[code=php]
<?php
include “incl.php”;
$connect();
$output();
?>
[/code]

but no luck, I get an error that says “Call to undefined function: ()” Now do I have to state the class and point it to the function? Any ideas or hints are welcomed! Thanks! ?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@chazzyApr 06.2006 — You're missing a step.

[code=php]
include "incl.php";
$dbconn = new TDatabase("host","user","password","database);
$dbconn->connect();
$dbconn->output();
[/code]
Copy linkTweet thisAlerts:
@vicpal25authorApr 06.2006 — okay i changed my code and its not getting the error but should the output() print out the values from the TDatabase function that you declared there?
[code=php]
<?php include "incl.php";?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
$dbconn = new TDatabase("host","user","password","database");
$dbconn->connect();
$dbconn->output();
?>
</body>
</html>
[/code]


I am getting an ouput but its not filling in the values that were placed in the new declaration.
Copy linkTweet thisAlerts:
@NogDogApr 06.2006 — You need to prepend your class variables names (class attributes) with "$this->", for example:
[code=php]
if ($this->isConnected)
// or //
echo "hostname={$this->host}...";
[/code]
×

Success!

Help @vicpal25 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...