Jump to content

mysql class - problems with close()


Wuhtzu

Recommended Posts

Hey guys

I have this very simple mysql class one of my friends wrote, but I am having problems with the close() function and I believe he has too if he tries to use it :)

[code]
class mysql {

public $connection;

function connect(){

$host = "lolol";
$username = "lolol";
$password = "lolol";
$database = "lolol";

$connection = mysql_connect($host, $username, $password) or die(mysql_error());
mysql_select_db("$database", $connection) or die(mysql_error());

}

function close(){
mysql_close($this->connection) or die(mysql_error());

}

        more functions that are irrelavant...

    }
[/code]

If I then try something like this:

[code]
$mysql = new mysql;
$mysql->connect();
$mysql->close();
[/code]

I get the following error:

Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in ...\classes\mysql.php on line 20

Line 20 is "mysql_close($this->connection) or die(mysql_error());" and don't worry, the connect() works fine - I can connect to my db without problem and insert data...

So what have I/he done wrong and how to fix it?

Best regards
Wuhtzu
Link to comment
https://forums.phpfreaks.com/topic/36437-mysql-class-problems-with-close/
Share on other sites


Here is the code I was testing, it's very simple and the solo purpose is to open a connection and close it again:

[code]
$mysql = new mysql;
$mysql->connect();
$mysql->close();
[/code]

And if I just use the mysql related code the mysql-class is made of it works fine:

[code]
$host = "localhost";
$username = "dan16068_engrosb";
$password = "2D4unGas";
$database = "dan16068_engrosbilar";

$connection = mysql_connect($host, $username, $password) or die(mysql_error());


if($connection){
echo "Succesfull connection";
}


$close = mysql_close($connection);

if($close){
echo "Connection closed";
}
[/code]

So the problem is how to get a valid MySQL-link resource into my mysql_close() ....
not much used to classes and writing them but it does not make sense to me to make a call to $this->connection when the connections is $connection so should it not be
[code]
function close(){
mysql_close($this->$connection) or die(mysql_error());

}
[/code]
So when you do
Can you try this?
function close(){
    print $this->connection;
    mysql_close() or die(mysql_error());
}

It should say something like Resource ID #, right?
You also don't need to supply the connection, so try it without.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.