Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/155867-solved-function-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/155867-solved-function-help/#findComment-820391
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/155867-solved-function-help/#findComment-820394
Share on other sites

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. 

Link to comment
https://forums.phpfreaks.com/topic/155867-solved-function-help/#findComment-820417
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/155867-solved-function-help/#findComment-820435
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/155867-solved-function-help/#findComment-820448
Share on other sites

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 ?

Link to comment
https://forums.phpfreaks.com/topic/155867-solved-function-help/#findComment-820460
Share on other sites

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!';
   }
}

Link to comment
https://forums.phpfreaks.com/topic/155867-solved-function-help/#findComment-820472
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/155867-solved-function-help/#findComment-820557
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.