Forum Moderators: coopster

Message Too Old, No Replies

How to properly close a mysql link

mysql_link, php

         

rodriguez1804

11:57 am on May 15, 2010 (gmt 0)

10+ Year Member



Hey guys, I have a simple class for connecting and disconnecting from the database, but I am not sure I am doing it right, because I get an error saying "mysql_close() is not a proper resource" warning.

Here's the code

class dbClass{
protected $conn="";

public function _constructor(){

}

public function connect()
{
$this->conn = mysql_connect(dbhost,dbuser,dbpass) or die ('Error connecting to mysql');
$selectedTbl = mysql_select_db(dbname) or die ('Error selecting database');
}

function closeConn() //when this function is called it's giving warning?
{
mysql_close($this->conn);
}
}


If anybody spots my mistake, let me know. Thanks.

Matthew1980

5:47 pm on May 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there rodriguez1804,

Have you echoed $this->conn to see if actually has a resource id# reference attached to it (means as the connection in there), because if not that's the problem, I personally don't think as you require the "protected $conn;" part, try it without to see if it makes any difference, because you have it defined as protected it may not work, though that just could be my poor understanding of that directive, logically speaking to me if something is protected you effectively are making it read only? I may be wrong there though.

If you don't use it you can still refer to the var using $this->conn as you are still referring to a var that is defined in the class, in this case the connection handle :)

Cheers,
MRb

Matthew1980

8:46 pm on May 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there rodriguez1804,

I have had a play with this myself as I hadn't got do doing a DB class before, after slightly tweeking yours, I have got this working:-

The complete PHP file.

<?php

Class dbClass{
//define vars
var $DBhost, $DBuser, $DBpass, $DB;

function connect(){

$conn = mysql_connect($this->DBhost,$this->DBuser,$this->DBpass) or die (mysql_error());

mysql_select_db($this->DB, $conn) or die (mysql_error());

return $conn;
}

function closeConn(){
mysql_close($this->connect());
}

}
?>

The 'index.php file':-

<?php
//Start error checking
error_reporting(E_ALL | E_STRICT);

//Include the class file and create instance
include('/class_file/dbClass.php');

$SQLConn = new dbClass();

//assign class vars
$SQLConn->DBpass = "yourpass";
$SQLConn->DBuser = "youruser";
$SQLConn->DBhost = "yourhost";
$SQLConn->DB = "yourDB";

$SqlQuery = "SELECT * FROM `yourtable` ";

$SqlSent = mysql_query($SqlQuery, $SQLConn->connect()) or die (mysql_error());

//
//Do what you need to with the results...
//

//Close the connection
$SQLConn->closeConn();
?>

This functions fine for me, notice though as I haven't done the protected directive on the function - basically as I'm not sure about that, so far as I am concerned get the class functional, then start to alter parts for your needs.

If anyone can advise me on how to use the protected directive before I scour php.net please do! Cheers!

Also, I have merged the connect & select DB functions, as I think its better use of vars to just declare the DB in a var, then just refer to it using the established connection. You will see what I mean when you try this yourself :)

Don't forget though that when using the "or die(mysql_error());" to remove it when you go live, because this has potential to reveal things about your DB to hackers - it does happen.

Hope this helps you - it's helped me!

Cheers,
MRb

rodriguez1804

3:13 am on May 17, 2010 (gmt 0)

10+ Year Member



lol, oh wow, I forgot to return the connection itself. No wonder it gave a cry about it not being a proper resource. Thanks a lot MRb.