ameer71 Posted January 18, 2011 Share Posted January 18, 2011 please tell me the reason that why mysql_fetch_array does not work inside a function for example the following works well in its current form $sql="SELECT * FROM $tbl_13"; $query=mysql_query($sql); while($rs=mysql_fetch_array($query)){ $p0 = $rs["pfid"]; $pm = $rs["pm"]; } but when i put it inside a function it gives me an error at function defone() { $sql="SELECT * FROM $tbl_13"; $query=mysql_query($sql); while($rs=mysql_fetch_array($query)){ $p0 = $rs["pfid"]; $pm = $rs["pm"]; } return $pm; } the error message is... Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in... Please identify the problem.. i'm an experience programmer in ruby and perl but new in php Quote Link to comment https://forums.phpfreaks.com/topic/224835-help-with-mysql_fetch_array-function/ Share on other sites More sharing options...
bh Posted January 18, 2011 Share Posted January 18, 2011 Its a problem with your query: $sql="SELECT * FROM $tbl_13"; In the function you dont define $tbl_13 variable (i think its a global) Quote Link to comment https://forums.phpfreaks.com/topic/224835-help-with-mysql_fetch_array-function/#findComment-1161308 Share on other sites More sharing options...
PFMaBiSmAd Posted January 18, 2011 Share Posted January 18, 2011 You already have an existing thread for this, where someone replied with the reason why your code does not work. In most programming languages, each function has its own isolated variable scope so that you can freely write any code and define any variables in the function that you need in order to accomplish the goal of that function, without needing to worry about overwriting any of the main program variables. Can you imagine how hard it would be to write a large application if you needed to keep track of all the variables you used to avoid any conflict between the main program and all the function definitions? You need to pass any values, such as your $tbl_13 variable, into the function as a parameter when you call the function. Quote Link to comment https://forums.phpfreaks.com/topic/224835-help-with-mysql_fetch_array-function/#findComment-1161309 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.