Trouble using mysql_query from inside a custom function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SplothchMan
Forum Newbie
Posts: 2
Joined: Mon Jul 27, 2009 4:29 pm

Trouble using mysql_query from inside a custom function

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Trouble using mysql_query from inside a custom function

Post by VladSun »

$connection is out of scope.
either use global to populate it in the function scope or pass it as argument.
There are 10 types of people in this world, those who understand binary and those who don't
SplothchMan
Forum Newbie
Posts: 2
Joined: Mon Jul 27, 2009 4:29 pm

Re: Trouble using mysql_query from inside a custom function

Post by SplothchMan »

VladSun,

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

Best Regards
Karthik
Post Reply