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
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() ....
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.