ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

$DB->insert_id always returns 0 :(

July 15, 2010 7:27am

Subscribe [2]
  • #1 / Jul 15, 2010 7:27am

    GlynD

    14 posts

    Hi guys 😊

    I’m trying to create a hidden member field to identify if members have paid their subscriptions. I’ve not been able to do it by creating a custom_field as members can change this in their profiles to say if they have paid or not.

    As a work around, I’ve created an extra table called exp_member_subs
    with two columns; member_id and subs_paid

    When a new user registers, I want to create an entry into the exp_member_subs table to record that their subscription is not paid. The subs_paid column has a default value of ‘no’.

    I’ve modified system/modules/member/mod.member_register.php

    /**  Insert basic member data */
    $DB->query($DB->insert_string('exp_members', $data));    
    $member_id = $DB->insert_id;
    
    /**  Insert subs */
    $DB->query($DB->insert_string('exp_member_subs', $member_id));

    It inserts the entry, but the member_id is always 0.

    Any ideas?

  • #2 / Jul 16, 2010 6:17am

    GlynD

    14 posts

    Having no joy with this, I’ve turned my attention to trying to perform a SQL SELECT query to query the last member_id in the table exp_members but can’t get this to work either :(

    <?php
       global $DB;
       $results = $DB->query("SELECT member_id FROM exp_members ORDER BY member_id DESC LIMIT 0, 1");
       $memberID =$results ->row('member_id');
       echo $memberID;
    ?>

    Can anybody tell me where I am going wrong here please?

    Thanks

    Glyn

  • #3 / Jul 16, 2010 9:28am

    GlynD

    14 posts

    This doesn’t work either :(

    <?php
       global $DB;
       $query = $DB->query("SELECT * FROM exp_members");
       if ($query->num_rows() > 0){
         echo "success";
       }
    ?>

    or this

    <?php
       $query = $this->EE->db->query("SELECT * FROM exp_members");
       if ($query->num_rows() > 0){
         echo "success";
       }
    ?>
  • #4 / Jul 16, 2010 3:36pm

    Gareth Davies

    491 posts

    Out of interest does this as it does on a full version of 1.6.9.

    <?php
       global $DB;
       $query = $DB->query("SELECT * FROM exp_members");
       if ($query->num_rows > 0){
         echo "success";
       }
    ?>

    Core does have limitations on the membership side of things so not entirely sure whether that could be the root cause of your issues.

  • #5 / Jul 19, 2010 6:05am

    GlynD

    14 posts

    Thanks Gareth, that worked. The problem now though is that I can’t work out how to retrieve the collected data.

    How would I output the member_id collected?

    <?php
       global $DB;
       $query = $DB->query("SELECT member_id FROM exp_members ORDER BY member_id DESC LIMIT 0, 1");
       if ($query->num_rows==1){
          echo $query->row(...);
       }
    ?>

    I’ve tried this, but the brackets appear to be causing problems again:

    echo $query->row()->member_id;

    Thanks again

    Glyn

  • #6 / Jul 19, 2010 1:44pm

    Gareth Davies

    491 posts

    This will work for the above query:

    <?php
       global $DB;
       $query = $DB->query("SELECT member_id FROM exp_members ORDER BY member_id DESC LIMIT 0, 1");
       if ($query->num_rows==1){
          echo $query->row['member_id'];
       }
    ?>
  • #7 / Jul 19, 2010 2:08pm

    GlynD

    14 posts

    Worked a treat. Thanks heaps :D

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases