BrandonLayton Posted December 16, 2010 Share Posted December 16, 2010 This is my error: Fatal error: Can't use function return value in write context in /home/a8152576/public_html/SecretSanta.html on line 41 confused about why. This is the line 41: if(isset($_POST('submit')){ this is the php of my page (without the mysql data): <?php class functions{ public static function chooseGiftee($SS){ $done = false; while($done=false){ $sqllink= mysql_connect($mysql_host,$mysql_user,$mysql_password); if(!$sqllink){ echo "Could not connect! Please Try Again"; mysql_close($sqllink); } else { $selectGiftee = "SELECT * FROM SecretSantas ORDER BY RAND() LIMIT 0,10;"; while($row=mysql_fetch_array($result)){ $results = mysql_query($selectGiftee); if(!$row['Gifter']=''){ $done=false; } else { $gifteeResults = $row['Name']; $addSS = "INSERT INTO SecretSantas (Gifter) VALUES ('" .$SS. "')"; $done=true; return $gifteeResults; } } } mysql_close($sqllink); } } } if(isset($_POST('submit')){ $echoName = functions::chooseGiftee($_POST['gifterName']); echo $echoName; } ?> This is html: <form id="form1" name="form1" method="post" action=""> <p> <label>Your Name: <input type="text" name="gifterName" id="gifterName" /> </label> <input type="submit" name="submit" id="submit" value="Submit" /> </p> <p> </p> </form> Confused about the issue, first time using functions. Thanks in advance, Brandon Quote Link to comment https://forums.phpfreaks.com/topic/221821-cant-use-function-return-value-in-write-context/ Share on other sites More sharing options...
trq Posted December 16, 2010 Share Posted December 16, 2010 The syntax for a function is.... function nameoffunction() { // code } Your code is a mixmatch of classes and functions that doesn't make allot of sense at all. Quote Link to comment https://forums.phpfreaks.com/topic/221821-cant-use-function-return-value-in-write-context/#findComment-1147929 Share on other sites More sharing options...
trq Posted December 16, 2010 Share Posted December 16, 2010 Sorry, I obviously completely misread your code (at work, with a few people around). Anyway, there really is no need for a class here at all. Classes shouldn't be used to simply group functions together, theres allot miore to them than that. I understand hwoeever that your likely just trying to learn the syntax. Theres quite a few errors with your code. Classes, like functions have there own scope. That means, all those arguments passed to mysql_connect() do not exist. Not likey the cause of your issue, but it's where I'm starting. Quote Link to comment https://forums.phpfreaks.com/topic/221821-cant-use-function-return-value-in-write-context/#findComment-1147932 Share on other sites More sharing options...
trq Posted December 16, 2010 Share Posted December 16, 2010 Having a better look at your code now. I would forget all about classes and functions for a while, you might need to revise some php basics. Your code really makes little sense. The entire process run in a while() loop, why? Your try and execute another while loop passing $result to mysql_fetch_array() yet $result is not defined. Within that loop you execute a query and then do nothing with $results. Really, it is all over the place. Quote Link to comment https://forums.phpfreaks.com/topic/221821-cant-use-function-return-value-in-write-context/#findComment-1147933 Share on other sites More sharing options...
BrandonLayton Posted December 16, 2010 Author Share Posted December 16, 2010 Ok thanks for the reply. Oops. Shouldn't have $results in the loop... and that loop should be an if statement if I am correct this time The purpose of the while loop is to make sure that the row does not already have a value for 'Gifter'. Not sure how else I can do that. As you can probably tell I am pretty new to php... Quote Link to comment https://forums.phpfreaks.com/topic/221821-cant-use-function-return-value-in-write-context/#findComment-1147936 Share on other sites More sharing options...
trq Posted December 16, 2010 Share Posted December 16, 2010 The purpose of the while loop is to make sure that the row does not already have a value for 'Gifter'. Not sure how else I can do that. Check for it in your query. Quote Link to comment https://forums.phpfreaks.com/topic/221821-cant-use-function-return-value-in-write-context/#findComment-1147939 Share on other sites More sharing options...
BrandonLayton Posted December 16, 2010 Author Share Posted December 16, 2010 This is my attempt: <?php public static function chooseGiftee($SS){ $done = false; $sqllink= mysql_connect($mysql_host,$mysql_user,$mysql_password); if(!$sqllink){ echo "Could not connect! Please Try Again"; mysql_close($sqllink); } else { $selectGiftee = "SELECT * FROM SecretSantas ORDER BY RAND() WHERE 'Gifter' = '' LIMIT 0,10"; $results = mysql_query($selectGiftee); $row=mysql_fetch_array($result) if(!$row['Gifter']=''){ $done=false; } else { $gifteeResults = $row['Name']; $addSS = "INSERT INTO SecretSantas (Gifter) VALUES ('" .$SS. "')"; $done=true; return $gifteeResults; } } mysql_close($sqllink); } ?> Error: Parse error: syntax error, unexpected T_PUBLIC in /home/a8152576/public_html/SecretSanta.html on line 14 That is on this line: public static function chooseGiftee($SS){ Little confused about why its saying it shouldn't be public. Quote Link to comment https://forums.phpfreaks.com/topic/221821-cant-use-function-return-value-in-write-context/#findComment-1147946 Share on other sites More sharing options...
trq Posted December 16, 2010 Share Posted December 16, 2010 Because it's not within a class. Quote Link to comment https://forums.phpfreaks.com/topic/221821-cant-use-function-return-value-in-write-context/#findComment-1147966 Share on other sites More sharing options...
BrandonLayton Posted December 16, 2010 Author Share Posted December 16, 2010 ok... I understand now. Thanks for all of your help Extremely appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/221821-cant-use-function-return-value-in-write-context/#findComment-1148403 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.