Lamez Posted February 8, 2008 Share Posted February 8, 2008 I have this code here, it is a function to see if the user is in one table and in the other table. anyways I have a function in the middle of the loop, I can only declare it once, but since it is a loop, it re-declares the function. here is my code: <? $qr=mysql_query("select * from users order by first"); $pt=mysql_query("select * from paid order by user"); while ($rs=mysql_fetch_assoc($qr)) { $pu=mysql_fetch_assoc($pt); $table = 'paid'; $field = 'user'; $compared = $pu['user']; function checkUnique($table, $field, $compared){ if (get_magic_quotes_gpc()) { $table = stripslashes($table); $field = stripslashes($field); $compared = stripslashes($compared); } $table = mysql_real_escape_string($table); $field = mysql_real_escape_string($field); $compared = mysql_real_escape_string($compared); $result = mysql_query("SELECT $field FROM $table WHERE $field = '$compared'"); if(mysql_num_rows($result)==0) { return TRUE; echo "Paid"; } else { return FALSE; echo "Not Paid"; } } How do I fix this to only declare once? I get this error: Fatal error: Cannot redeclare checkunique() (previously declared in /mounted-storage/home48c/sub007/sc33591-LWQU/lamezz.info/_admin/bracket/paid.php:25) in /mounted-storage/home48c/sub007/sc33591-LWQU/lamezz.info/_admin/bracket/paid.php on line 25 Quote Link to comment https://forums.phpfreaks.com/topic/90002-solved-loop-w-function/ Share on other sites More sharing options...
emehrkay Posted February 8, 2008 Share Posted February 8, 2008 just move it outside of the loop Quote Link to comment https://forums.phpfreaks.com/topic/90002-solved-loop-w-function/#findComment-461482 Share on other sites More sharing options...
kenrbnsn Posted February 8, 2008 Share Posted February 8, 2008 Move the function declaration out of the loop. Put it at the top of your script. Ken Quote Link to comment https://forums.phpfreaks.com/topic/90002-solved-loop-w-function/#findComment-461484 Share on other sites More sharing options...
Lamez Posted February 8, 2008 Author Share Posted February 8, 2008 Right, but how do I assign the username? It pulls it out of the loop. I am confused Quote Link to comment https://forums.phpfreaks.com/topic/90002-solved-loop-w-function/#findComment-461485 Share on other sites More sharing options...
kenrbnsn Posted February 8, 2008 Share Posted February 8, 2008 You have to invoke the function from within the loop. Also, the "echo" statement in the function after the return statements will not be executed, since the return statement ends the execution of the function. Looking at the function, I don't think you really need a to use a function here, just make the code in-line. <?php $qr=mysql_query("select * from users order by first"); $pt=mysql_query("select * from paid order by user"); while ($rs=mysql_fetch_assoc($qr)) { $pu=mysql_fetch_assoc($pt); $table = 'paid'; $field = 'user'; $compared = $pu['user']; $result = mysql_query("SELECT $field FROM $table WHERE $field = '$compared'"); if(mysql_num_rows($result)==0) { echo "Paid"; } else { echo "Not Paid"; } } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/90002-solved-loop-w-function/#findComment-461495 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.