By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,373 Members | 1,792 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,373 IT Pros & Developers. It's quick & easy.

mysql_close(): supplied argument is not a valid MySQL-Link resource

P: n/a
I have a db class that sets up a connection. It then has methods to
query the db and fetch results etc that encapsulate the normal mysql
functions (i.e. mysql_query($sql)).

I seem to get this error only when I try and create a new copy of this
class when one already is open.

For instance I query a db to get all the event ids from the table. I
then go through an load each event from the id. Because the query is
being issued whilst a connection is already open the resources seem to
be getting confused.

Below is my db code and the example.

class Database{

private $conn;

function __construct(){
$this->conn = mysql_connect(DB_SERVER, DB_SERVER_USERNAME,
DB_SERVER_PASSWORD)
or die('Could not connect: ' . mysql_error());
if($this->conn){
mysql_select_db(DB_DATABASE) or die('Could not connect: ' .
mysql_error());
}
}

function query($sql){
$query = mysql_query($sql);
if(!$query){
die("Error: in database query");
}else{
return $query;
}
}

function fetch_array($result){
$array = mysql_fetch_assoc($result);
return $array;
}

function close(){
mysql_close($this->conn);
$this->conn = null;
}

function __destruct(){
$this->close();
}
}

Example

function get_events($where = null){
$db = new Database();
$sql = "SELECT * FROM event ";
if(isset($where)){
$sql .= $where;
}
$query = $db->query($sql);
$res = array();
while($result = $db->fetch_array($query)){
$event = new Event($result['id']);
$res[] = $result;
}
return $res;
}

//Loads an event from the database using the id as primary key
function load($id){
//Get the event from the database
$db = new Database();
$sql = sprintf("SELECT * FROM event WHERE id = %u", $id);
$query = $db->query($sql);
$result = $db->fetch_array($query);

load is called from constructor.

If anyone has an idea please please help this problem is very annoying.
Dec 12 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Greetings, Iain Adams.
In reply to Your message dated Wednesday, December 12, 2007, 18:55:27,
I have a db class that sets up a connection. It then has methods to
query the db and fetch results etc that encapsulate the normal mysql
functions (i.e. mysql_query($sql)).
I seem to get this error only when I try and create a new copy of this
class when one already is open.
For instance I query a db to get all the event ids from the table. I
then go through an load each event from the id. Because the query is
being issued whilst a connection is already open the resources seem to
be getting confused.
Below is my db code and the example.
class Database{
private $conn;
function __construct(){
$this->conn = mysql_connect(DB_SERVER, DB_SERVER_USERNAME,
DB_SERVER_PASSWORD)
or die('Could not connect: ' . mysql_error());
if($this->conn){
mysql_select_db(DB_DATABASE) or die('Could not connect: ' .
mysql_error());
}
}
function query($sql){
$query = mysql_query($sql);
if(!$query){
die("Error: in database query");
}else{
return $query;
}
}
function fetch_array($result){
$array = mysql_fetch_assoc($result);
return $array;
}
function close(){
mysql_close($this->conn);
$this->conn = null;
}
function __destruct(){
$this->close();
}
}
Example
function get_events($where = null){
$db = new Database();
$sql = "SELECT * FROM event ";
if(isset($where)){
$sql .= $where;
}
$query = $db->query($sql);
$res = array();
while($result = $db->fetch_array($query)){
$event = new Event($result['id']);
$res[] = $result;
}
return $res;
}
//Loads an event from the database using the id as primary key
function load($id){
//Get the event from the database
$db = new Database();
$sql = sprintf("SELECT * FROM event WHERE id = %u", $id);
$query = $db->query($sql);
$result = $db->fetch_array($query);
load is called from constructor.
If anyone has an idea please please help this problem is very annoying.
I have an idea.. Why do You use class to do this things?
You coding style is very naive and You're using alot of defaults in Your code.

If You REALLY need to get it to work, go studying the link_identifier
parameter of mysql_* functions.

After that, Your code should looks like

class Database
{

protected $conn; // do not use 'private' if You aren't absolutely sure You want it

function __construct()
{
$this->conn = mysql_connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD)
or die('Could not connect: ' . mysql_error());
if(is_resource($this->conn))
{
mysql_select_db(DB_DATABASE, $this->conn) or die('Could not connect: ' . mysql_error());
}
}

function query($sql)
{
$query = mysql_query($sql, $this->conn);
if(!is_resource($query))
{
die("Error: in database query");
}
else
{
return $query;
}
}

function fetch_array($result)
{
$array = mysql_fetch_assoc($result);
return $array;
}

function close()
{
if(mysql_close($this->conn))
{
$this->conn = null;
}
}

function __destruct()
{
$this->close();
}
}
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Dec 12 '07 #2

P: n/a
Iain Adams wrote:
I have a db class that sets up a connection. It then has methods to
query the db and fetch results etc that encapsulate the normal mysql
functions (i.e. mysql_query($sql)).

I seem to get this error only when I try and create a new copy of this
class when one already is open.

For instance I query a db to get all the event ids from the table. I
then go through an load each event from the id. Because the query is
being issued whilst a connection is already open the resources seem to
be getting confused.

Below is my db code and the example.

class Database{

private $conn;

function __construct(){
$this->conn = mysql_connect(DB_SERVER, DB_SERVER_USERNAME,
DB_SERVER_PASSWORD)
or die('Could not connect: ' . mysql_error());
if($this->conn){
mysql_select_db(DB_DATABASE) or die('Could not connect: ' .
mysql_error());
}
}

function query($sql){
$query = mysql_query($sql);
if(!$query){
die("Error: in database query");
}else{
return $query;
}
}

function fetch_array($result){
$array = mysql_fetch_assoc($result);
return $array;
}

function close(){
mysql_close($this->conn);
$this->conn = null;
}

function __destruct(){
$this->close();
}
}

Example

function get_events($where = null){
$db = new Database();
$sql = "SELECT * FROM event ";
if(isset($where)){
$sql .= $where;
}
$query = $db->query($sql);
$res = array();
while($result = $db->fetch_array($query)){
$event = new Event($result['id']);
$res[] = $result;
}
return $res;
}

//Loads an event from the database using the id as primary key
function load($id){
//Get the event from the database
$db = new Database();
$sql = sprintf("SELECT * FROM event WHERE id = %u", $id);
$query = $db->query($sql);
$result = $db->fetch_array($query);

load is called from constructor.

If anyone has an idea please please help this problem is very annoying.
Iain,

Your class looks fine. However, I suspect what's happening is your
__destruct() function is getting called when you don't expect it. This
is often done when you pass something by copy instead of by reference.

Put an echo in your __destruct() function (and maybe even in close())
and see if it isn't being called when you don't expect it to be called.

And BTW - private is correct for your connection. protected should be
used only when absolutely necessary, if even then.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Dec 13 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.