Page 1 of 1

Trouble using mysql_query from inside a custom function

Posted: Mon Jul 27, 2009 4:56 pm
by SplothchMan
Hi,

I created the function below to delete a record from a MySQL table.

function Del_record($table, $id){
$query_del_record = "DELETE FROM $table WHERE $table.id = \"$id\"";
$del_record = mysql_query($query_del_record, $connection) or die(mysql_error());
}

When the function is called execution stops at the mysql_query statement. However, when I removed the function structure and pasted the code directly at the calling location, it worked fine. Moreover I have the same problem with all other custom defined functions where I've used mysql_query() irrespective of the operation involved (SELECT, INSERT, UPDATE, DELETE). Pasting the code inline is not the ideal solution for me as quite a few of the functions I'll need to use frequently are very lengthy and a hassle to reconfigure.

Is there a problem or limitation with the mysql_query() function that I am unaware of? Can anybody help make my function work? My apologies if this is a rehash of an old question, I haven't been able to find an answer after almost a whole day of searching online. Thanks in advance for your help.

Best Regards
Karthik

Re: Trouble using mysql_query from inside a custom function

Posted: Mon Jul 27, 2009 4:58 pm
by VladSun
$connection is out of scope.
either use global to populate it in the function scope or pass it as argument.

Re: Trouble using mysql_query from inside a custom function

Posted: Mon Jul 27, 2009 5:16 pm
by SplothchMan
VladSun,

Wow, that was fast. I took your advice and it works now. Thank you so much.

Best Regards
Karthik