Jump to content

mysql_close($con) is not working in one of my functions


Recommended Posts

I have a lot of functions in my PHP file doing database queries... but this one for some reason my debugger is saying it can't close the connection

 

The debugger statement is the following.

---------------------------

mysql_close(): supplied argument is not a valid MySQL-Link resource in <b>....etc, gives a path name and points to specific code line....

----------------

Of course if I take out the close statement it doesn't choke, but I'm worried some queries are getting left open running on my server..... since things seem to be slowing down quickly.

 

 

 

function changeCell($param)

{

 

$link = getConection();

if (!$link)

{

die('Could not connect: ' . mysql_error());

}

mysql_select_db('MyDatabase');

$sql_select_current_change = "select * from data where id=" . $param['dataid'];

 

 

$query = mysql_query($sql_select_current_change);

$result = mysql_fetch_array($query);

mysql_close($con);

$change_status = $result['ISCLICK_NUMBER2'];

if ($change_status >0){

 

runNumbers();

 

}

$change_status++;

 

$sql_change_click = "UPDATE data set ISCLICK_NUMBER2='$change_status' where id=" . $param['dataid'];

$result = mysql_query($sql_change_click);

mysql_close($con);

if ($result)

return $param['dataid'] . '_' . $change_status;

else

return "false";

}

A) You have a wrong variable name in your mysql_close statement, which is why it isn't a link resource. The error message probably told you it was a null/non-existent value.

 

B) You should NOT be opening/closing database connections more than ONCE on any page. Each time you open a database connection, it takes a significant amount of time to contact the database server, authenticate the database username/password, and obtain a connection handle. You should open a connection once on any page that needs it, then either close it after you are done using it on that page, or more easily, let php close the connection when it automatically destroys all the resources used on any page.

Thanks for the help. I hired someone to create the page for me, so it looks like there were some things not cleaned up.

 

The last statement you made, about closing the connection only when PHP destroys all the resources used on any page.... Is this done automatically or do I need to put some command in to detect when the resources are destroyed, and then do the close?

 

Thanks

It's also kind of fortunate that the person who wrote that code managed to get the wrong variable name in the mysql_close statement, because if the variable name had been correct, the UPDATE query would fail because there would be no database connection at that point.

 

In this case, having a programmer that didn't know what he was doing actually produced code that 'worked'.

Edited by PFMaBiSmAd
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.