MasterACE14 Posted March 21, 2008 Share Posted March 21, 2008 hey guys, I'm having some trouble with my foreach loop. I'm getting a "invalid argument" error with it. If anyone can point out where I have gone wrong, it would be greatly appreciated. Thanks code: <?php $sql = "SELECT * FROM cf_users,cf_users2 WHERE cf_users.id='$playerid' LIMIT 1"; $rs = @mysql_query($sql) or die('Query:<br />' . $sql . '<br /> ' . $playerid . ' <br />Error:<br />' . mysql_error()); $row = mysql_fetch_array($rs); foreach ($row as $colname=>$value) { $player_{$colname} = $value; } return $player_{$colname}; ?> error: Warning: Invalid argument supplied for foreach() in /home/ace/public_html/conflictingforces/functions.php on line 20 line 20 is the foreach loop line. Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/97200-foreach-loop-invalid-argument/ Share on other sites More sharing options...
Jeremysr Posted March 21, 2008 Share Posted March 21, 2008 Try putting print_r($row); right after assigning $row to see what's in it. That'll help you (and us) to find the problem. Also you should use mysql_fetch_assoc() instead of mysql_fetch_array() when you want to loop through it like that. This is because mysql_fetch_array() returns (by default) an array with duplicate values, one with a number as the key, and one with the column name as the key. For example: $result = mysql_query("SELECT username, country FROM users"); $values = mysql_fetch_array($result); print_r($values); would print something like Array(0 => 'Jeremysr', 'username' => 'Jeremysr', 1 => 'Canada', 'country' => 'Canada') But this: $result = mysql_query("SELECT username, country FROM users"); $values = mysql_fetch_assoc($result); print_r($values); would print Array('username' => 'Jeremysr', 'country' => 'Canada') Quote Link to comment https://forums.phpfreaks.com/topic/97200-foreach-loop-invalid-argument/#findComment-497367 Share on other sites More sharing options...
MasterACE14 Posted March 21, 2008 Author Share Posted March 21, 2008 heres what print_r($row); output. Array ( [id] => 35 [username] => ACE [password] => my_password => ace@crikeygames.com.au [verify] => 0 [race] => 3 [money] => 8750 [bank] => 22815 [level] => 8 [currentexp] => 114588 [neededexp] => 131685 [authlevel] => 2 [currenthealth] => 170 [currentpower] => 170 [maxhealth] => 170 [maxpower] => 170 [skilllevel] => 1 [skillpoints] => 0 [strength] => 34 [agility] => 33 [intelligence] => 34 [strikeaction] => 404593 [defenceaction] => 319586 [covertaction] => 422099 [talentid] => 0 [talentname] => none [talentlevel] => 1 [talentpoints] => 0 [weaponid] => 0 [weapons] => 0 [armorid] => 0 [armors] => 0 [vehicleid] => 0 [vehicles] => 0 [allianceid] => 0 [alliancename] => none [allianceposition] => 0 [regdate] => 2007-09-25 20:11:35 [ammo] => 1443 [rank] => 18 [price_strikeops] => 92500 [amount_strikeops] => 19000 [level_strikeops] => 19 [level_defenceops] => 16 [amount_defenceops] => 16000 [price_defenceops] => 40000 [price_covertops] => 50000 [amount_covertops] => 20000 [level_covertops] => 20 [score] => 382092 [level_supplysize] => 1 [level_supplydrop] => 1 [amount_supplydrop] => 1 [amount_supplysize] => 100 [price_supplydrop] => 1000000 [price_supplysize] => 250 [title] => Player ) Quote Link to comment https://forums.phpfreaks.com/topic/97200-foreach-loop-invalid-argument/#findComment-497369 Share on other sites More sharing options...
MasterACE14 Posted March 21, 2008 Author Share Posted March 21, 2008 bump Quote Link to comment https://forums.phpfreaks.com/topic/97200-foreach-loop-invalid-argument/#findComment-497383 Share on other sites More sharing options...
Orio Posted March 21, 2008 Share Posted March 21, 2008 At a fitst glance this seems fine... What are you trying to do in this function? After the loop you are returning $player_{$colname}, but $colname is not defined outside of the foreach()'s scope. If you'd define exactly what you want this function to do, maybe it'd be easier to rewrite it. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/97200-foreach-loop-invalid-argument/#findComment-497386 Share on other sites More sharing options...
MasterACE14 Posted March 21, 2008 Author Share Posted March 21, 2008 I want it to place each columns value into the variable, and have the column name as the variable name suffix. Quote Link to comment https://forums.phpfreaks.com/topic/97200-foreach-loop-invalid-argument/#findComment-497421 Share on other sites More sharing options...
Orio Posted March 21, 2008 Share Posted March 21, 2008 In the foreach you are creating many variables. But in the end of the function you are only returning one value, and that variable itself you are returning doesn't exists because $colname doesn't exist outside of the foreach's scope... I can't really understand why your foreach is throwing a warning, but the function itself doesn't do what you are saying. I also don't really understand why you are creating so many variables, that's what arrays are for (especially in php, where keys can be strings). Orio. Quote Link to comment https://forums.phpfreaks.com/topic/97200-foreach-loop-invalid-argument/#findComment-497523 Share on other sites More sharing options...
MasterACE14 Posted March 21, 2008 Author Share Posted March 21, 2008 I've revised my code and rather then creating variables, I'm going for the array approach, however I am stuck on this error... Warning: Invalid argument supplied for foreach() in /home/ace/public_html/conflictingforces/functions.php on line 10 Warning: Invalid argument supplied for foreach() in /home/ace/public_html/conflictingforces/functions.php on line 10 Warning: Invalid argument supplied for foreach() in /home/ace/public_html/conflictingforces/functions.php on line 10 Warning: Invalid argument supplied for foreach() in /home/ace/public_html/conflictingforces/functions.php on line 10 here's my new code, and how I call it. <?php // Select fields from other users table function player_table($id=null){ if($id == null) { $id = $_SESSION['playerid']; } $sql = "SELECT * FROM cf_users,cf_users2 WHERE cf_users.id= '$id' && cf_users2.id= '$id' LIMIT 1"; $rs = @mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); $row = mysql_fetch_assoc($rs); $player = array(); foreach ($row as $colname=>$value) { $player[$colname] = $value; } } $player = player_table($_SESSION['playerid']); echo $player['id']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/97200-foreach-loop-invalid-argument/#findComment-497865 Share on other sites More sharing options...
Jeremysr Posted March 21, 2008 Share Posted March 21, 2008 I still have no idea how that would cause an error, but what that foreach() statement does (or should be doing) is just copying the array, one element at a time which is unnecessary. Try this instead. (I added the return statement that you seem to be missing.) // Select fields from other users table function player_table($id=null){ if($id == null) { $id = $_SESSION['playerid']; } $sql = "SELECT * FROM cf_users,cf_users2 WHERE cf_users.id= '$id' && cf_users2.id= '$id' LIMIT 1"; $rs = @mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); $row = mysql_fetch_assoc($rs); return $row; } Quote Link to comment https://forums.phpfreaks.com/topic/97200-foreach-loop-invalid-argument/#findComment-497869 Share on other sites More sharing options...
MasterACE14 Posted March 21, 2008 Author Share Posted March 21, 2008 lol, that works perfectly. So I didn't need any of that foreach code at all? Quote Link to comment https://forums.phpfreaks.com/topic/97200-foreach-loop-invalid-argument/#findComment-497870 Share on other sites More sharing options...
Orio Posted March 22, 2008 Share Posted March 22, 2008 No, you didn't need that. But I still wonder what the problem was... Orio. Quote Link to comment https://forums.phpfreaks.com/topic/97200-foreach-loop-invalid-argument/#findComment-498094 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.