EchoFool Posted April 27, 2009 Share Posted April 27, 2009 Im having problems returning a mysql fetch assoc in my function to use on a page and am unsure on how to approach it. Currently i have this: <?php function itemwepname($Type,$ObjectID){ If($Type == 'Item'){ $Table = 'item'; $Field = 'ItemID'; $Collect = mysql_query("SELECT Name,Size FROM $Table WHERE $Field='$ObjectID'") Or die(mysql_error()); }ElseIf($Type == 'Weapon'){ $Table = 'weapons'; $Field = 'WeaponID'; $Collect = mysql_query("SELECT Name FROM $Table WHERE $Field='$ObjectID'") Or die(mysql_error()); } $row = mysql_fetch_assoc($Collect); return ($row); } <?php function itemweprecieve($Type,$ObjectID,$Quantity){ $row = itemwepname($Type,$ObjectID); If($Type == 'Item'){ bagspace(); $Array = bagspace(); $Free = $Array[0]; If($row['Size']*$Quantity > $Free){ //ERROR IS COMING FROM HERE Echo 'You do not have enough bag space!'; } The problem lies with the return of $row, its not acting like its returning the mysql fetch assoc, meaning in my second function i get this as an error: Notice: Undefined index: Size Notice: Undefined index: Size I was trying to avoid an array return by just returning mysql_fech_assoc but i cannot get it to work, is my approach even possible? Quote Link to comment https://forums.phpfreaks.com/topic/155867-solved-function-help/ Share on other sites More sharing options...
mrMarcus Posted April 27, 2009 Share Posted April 27, 2009 that's telling you that Size is not a set value. Quote Link to comment https://forums.phpfreaks.com/topic/155867-solved-function-help/#findComment-820388 Share on other sites More sharing options...
premiso Posted April 27, 2009 Share Posted April 27, 2009 If(isset($row['Size']) && $row['Size']*$Quantity > $Free){ //ERROR IS COMING FROM HERE Should solve the issue. What is going on is if your function hits the ElseIf, no Size is returned, so in essence you are setting yourself up for a failure. Adding that to the IF would prevent this error message. Quote Link to comment https://forums.phpfreaks.com/topic/155867-solved-function-help/#findComment-820391 Share on other sites More sharing options...
EchoFool Posted April 27, 2009 Author Share Posted April 27, 2009 that would defeat the point of my probielm here The function returns: <?php $row = mysql_fetch_assoc($Collect); return ($row); And as the query collects the field name Size,Name withing that function Wouldn't that allow me to use $row['Size']; when called from the script like : <?php $row = itemwepname($Type,$ObjectID); $row['Size']; Because at the moment its not working, so was curious if my logic is wrong here. Quote Link to comment https://forums.phpfreaks.com/topic/155867-solved-function-help/#findComment-820394 Share on other sites More sharing options...
premiso Posted April 27, 2009 Share Posted April 27, 2009 Then the answer is you need to pull size from your elseif query. Change the elseif query: ElseIf($Type == 'Weapon'){ $Table = 'weapons'; $Field = 'WeaponID'; $Collect = mysql_query("SELECT Name FROM $Table WHERE $Field='$ObjectID'") Or die(mysql_error()); } To be this: ElseIf($Type == 'Weapon'){ $Table = 'weapons'; $Field = 'WeaponID'; $Collect = mysql_query("SELECT Size,Name FROM $Table WHERE $Field='$ObjectID'") Or die(mysql_error()); } As you were not pulling Size from that query before, hence if it ever hit that elseif your loop would not see Size as being populated because you omitted from being returned in that SQL. If, however, you do not want size returned if it is a weapon, than you want to use the isset, as you can have Size returned if the first if is true, and you can have it not returned if it hits the elseif. The simple isset solves this issue that if Size was not returned, due to the function going into the elseif, then it will not throw an error. Quote Link to comment https://forums.phpfreaks.com/topic/155867-solved-function-help/#findComment-820417 Share on other sites More sharing options...
EchoFool Posted April 27, 2009 Author Share Posted April 27, 2009 How ever in this case Type was 'Item' not weapon.. weapons do not have a size value only items . In short to simplify my script this is what i was attempting to do: Simplified version: <?php function itemwepname($Type,$ObjectID){ $Table = 'item'; $Field = 'ItemID'; $Collect = mysql_query("SELECT Name,Size FROM $Table WHERE $Field='$ObjectID'") Or die(mysql_error()); $row = mysql_fetch_assoc($Collect); return ($row); } <?php function itemweprecieve($Type,$ObjectID,$Quantity){ $row = itemwepname($Type,$ObjectID); If($row['Size'] > 10){ Echo 'You do not have enough bag space!'; } ?> Im returning the $row and then just echo'ing rows but for some reason it doesn't like my returning row in that way, thus im wonderinf if its even possible or i have to use arrays when returning? Quote Link to comment https://forums.phpfreaks.com/topic/155867-solved-function-help/#findComment-820435 Share on other sites More sharing options...
premiso Posted April 27, 2009 Share Posted April 27, 2009 Well here is the question, are you sure that a row was returned for the query? A query can return 0 rows. Perhaps add a mysql_num_rows check inside the function and echo it out to see. As 0 rows is valid and not considered an error. And if 0 rows are returned, perhaps return false, and if the return value is false, continue the loop, since no processing can be done. Quote Link to comment https://forums.phpfreaks.com/topic/155867-solved-function-help/#findComment-820448 Share on other sites More sharing options...
EchoFool Posted April 27, 2009 Author Share Posted April 27, 2009 Good thinking, I added: <?php If(mysql_num_rows($Collect)<1){ Echo 'No Rows'; }Else{ $row = mysql_fetch_assoc($Collect); Echo 'RowsFound'; return ($row); } ?> And had "RowsFound" echo'd. Im going to have a use an array to return the field values aint i from how it looks ? Quote Link to comment https://forums.phpfreaks.com/topic/155867-solved-function-help/#findComment-820460 Share on other sites More sharing options...
premiso Posted April 27, 2009 Share Posted April 27, 2009 function itemwepname($Type,$ObjectID){ If($Type == 'Item'){ $Table = 'item'; $Field = 'ItemID'; $Collect = mysql_query("SELECT Name,Size FROM $Table WHERE $Field='$ObjectID'") Or die(mysql_error()); }ElseIf($Type == 'Weapon'){ $Table = 'weapons'; $Field = 'WeaponID'; $Collect = mysql_query("SELECT Name FROM $Table WHERE $Field='$ObjectID'") Or die(mysql_error()); } if (mysql_num_rows($Collect) < 1) { return false; } $row = mysql_fetch_assoc($Collect); return ($row); } <?php function itemweprecieve($Type,$ObjectID,$Quantity){ $row = itemwepname($Type,$ObjectID); if (!is_array($row)) { echo "No rows returned.<br />"; // maybe a return false; here as well.... }ElseIf($Type == 'Item'){ bagspace(); $Array = bagspace(); $Free = $Array[0]; If($row['Size']*$Quantity > $Free){ //ERROR IS COMING FROM HERE Echo 'You do not have enough bag space!'; } } Quote Link to comment https://forums.phpfreaks.com/topic/155867-solved-function-help/#findComment-820472 Share on other sites More sharing options...
EchoFool Posted April 27, 2009 Author Share Posted April 27, 2009 Hey thanks for the replies and help. I tried your script but didn't work.. how ever i found the solution for any one else in my same predicament, the only way for it to work is to do this: <?php $row = mysql_fetch_assoc($Collect); $array = array($row['Name'],$row['Size']); return $array; Then to retrieve the info: <?php $Array = itemwepname($DropType,$DropID); $Name = $Array[0]; $Size = $Array[1]; I think my approach with the row return probably is not possible in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/155867-solved-function-help/#findComment-820557 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.