/    Sign up×
Community /Pin to ProfileBookmark

OOP Class can’t close connection

I am trying to write a function that closes the connection created in the following class:

[code=php]
class dbConnect {

function connect($dataB) {
$conn=mysql_connect(“127.0.0.1″,”username”,”password”) or die (mysql_error());
$db=mysql_select_db($dataB,$conn) or die (“Unable to connect to database1”);
}

function closeConnect($conn) {
mysql_close($conn);
}
}
[/code]

to open a connection, I type:

[code=php]
$connection1-> new dbConnect();
$sql=$connection1->connect($dataB);
[/code]

I know the function closeConnect() isn’t working. What will make it work?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiMar 09.2010 — Well, you need to call it for it to run. If you want it called automatically at the end of execution (which is not necessary btw wince PHP will close the connection for you) you could call it from a destructor method.

Also you are assigning the result of the connect method to a variable but the method doesn't return anything.

You might also want to consider storing the link identifier as a class property instead of passing it around as an argument to methods. Currently you could only ever call closeConnect() from within connect() since the link identifier variable only exists within the scope of that method.
Copy linkTweet thisAlerts:
@themonkey40authorMar 09.2010 — Mindzai could you give me an example?
Copy linkTweet thisAlerts:
@NogDogMar 09.2010 — Here's a quick (and untested) cut at how you might approach it:
[code=php]
<?php
class dbConnect
{
public $conn;
private $dbHost = "127.0.0.1";
private $dbUser = "username";
private $dbPassword = "password";
function connect($dataB)
{
$this->conn = mysql_connect($this->dbHost,
$this->dbUser,
$this->dbPassword);
if ($this->conn == false) {
throw new Exception(mysql_error());
}
$db = mysql_select_db($dataB, $conn);
if ($db == false) {
throw new Exception("Unable to connect to database1");
}
}
function closeConnect()
{
if (!empty($this->conn)) {
mysql_close($this->conn);
}
}
function __destruct()
{
$this->closeConnect();
}
}
[/code]
Copy linkTweet thisAlerts:
@NogDogMar 09.2010 — PS: You could use the MySQL[b]i[/b] extension in object-oriented mode and not have to create your own class. ?
×

Success!

Help @themonkey40 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.18,
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,
)...