SQL in Hook not working correctly.

Discussion in 'Customization & add-ons' started by hdmann, Nov 4, 2009.

  1. hdmann

    hdmann New Member

    Joined:
    Nov 2, 2009
    Messages:
    3
    I'm just starting to get a handle on amember so if I'm missing something simple - please forgive me.

    Ok - I've added a field 'family_id' to the members table. When a new member registers I want to see what the highest family_id is, increment by 1 and update the family_id for the new member with that value (I don't want to auto-increment the field because I only want certain members to have family_id's).

    So in site.inc.php I have the following code

    Code:
    function fwp($payment_id){   
        global $db;
        $payment = $db->get_payment($payment_id); // $payment is now an array
    	$u = $db->get_user($payment['member_id']);
    	$fid = $db->query($sql = "SELECT MAX(family_id) FROM {$db->config[prefix]}members");
    	$u['family_id'] = $fid+1;
    	$db->update_user($payment['member_id'], $u);
    }
    setup_plugin_hook('finish_waiting_payment', 'fwp');
    The problem is that instead of adding the incremental value to the family_id field it puts 65 every time.

    If I run the sql directly on the database I get the correct value.

    Any ideas on what I'm doing wrong would be most appreciated.

    Thanks,
    Heather
  2. hdmann

    hdmann New Member

    Joined:
    Nov 2, 2009
    Messages:
    3
    I just tried printing out $fid and got this...Resource id #64
    hmmm.
  3. hdmann

    hdmann New Member

    Joined:
    Nov 2, 2009
    Messages:
    3
    Solved...

    Silly mistake - I needed to iterate through the array that was returned to get the value. As I had it it was just returning a pointer to the array. For someone else who might do the same thing, here's how it looks fixed.
    Code:
    function fwp($payment_id){   
        global $db;
        $payment = $db->get_payment($payment_id); // $payment is now an array
    	$u = $db->get_user($payment['member_id']);
    	$fid = $db->query($sql = "SELECT MAX(family_id) as fid FROM {$db->config[prefix]}members");
    	while ($row = mysql_fetch_array($fid, MYSQL_ASSOC)) {
    		$u['family_id'] = $row['fid']+1;
    	}
    	$db->update_user($payment['member_id'], $u);
    }
    setup_plugin_hook('finish_waiting_payment', 'fwp');
    Heather

Share This Page