The Zenbership environment is always available within a hook, so you have access to all Zenbership methods and classes. That allows you to easily load data from the Zenbership database as follows:

  • Learn more about the variables used in these examples

    Important

    Please be very careful when making raw SQL queries. The program makes NO ASSUMPTIONS about what you are sending to it, and will not clean SQL statements sent to it. All security precautions are on you! You can use the $db->mysql_cleans($value) method to help with this.

    Example 1: Getting All Event Registrations for a Member ID

    <?php
    
    $db = new db;
    
    $query = "
        SELECT *
        FROM ppSD_event_rsvps
        WHERE `user_id`='" . $db->mysql_cleans($member_id) . "'
        ORDER BY `date` DESC
    ";
    
    $results = $db->run_query($query);
    
    // Loop through the resulting rows...
    while ($anRsvp = $results->fetch()) {
        // var_dump($anRsvp);
        // your code here...
    }
    
    

    Example 2: Connecting to A Different Database

    • The db class does not care what database it is connected to, it only cares about making queries to whatever database that might be. So when connecting to a non-Zenbership database, with which Zenbership maintains an open connection to during the lifecycle of the program, you have to make sure you RECONNECT to the Zenbership database after your query!
    • If you are building a plugin, you don't need to fully connect to the database; you can instead use the helper methods within the plugin class: $plugin->connectLocal(); and $plugin->connectZen();

    Within a Plugin

    • For more context, you can take a look at the MyBB plugin, specifically the register hook which uses this DB connection method.
    <?php
    $plugin = new plugin('my_plugin');
    
    $plugin->connectLocal();
    
    // Do something here now that we are connected to the outside database...
    
    $plugin->connectZen();
    

    Within a Stand Alone Hook

    <?php
    
    // Connect to your non-Zenbership database
    $myDbHost = 'localhost';
    $myDbName = 'some_none_zenbership_database_name';
    $myDbUser = 'my_username';
    $myDbPassword = 'top_secret!';
    
    $DBH = new PDO(
        "mysql:host=" . $myDbHost . ";
        dbname=" . $myDbName,
        $myDbUser,
        $myDbPassword,
        array(
            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8;"
        )
    );
    
    $db = new db;
    $query = "Your SQL Query Here...";
    $results = $db->run_query($query);
    
    // Now connect back to Zenbership!
    $DBH = new PDO(
        "mysql:host=" . PP_MYSQL_HOST . ";
            dbname=" . PP_MYSQL_DB,
        PP_MYSQL_USER,
        PP_MYSQL_PASS,
        array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
    );
    
  • Last modified 4 months ago.