Query aMember database from WordPress

Discussion in 'aMember Pro v.4' started by charac_1, Sep 1, 2012.

  1. charac_1

    charac_1 New Member

    Joined:
    Aug 23, 2012
    Messages:
    19
    I need to query the aMember Pro database, am_user table, from within my WordPress functions.php file. Does anyone know if WordPress has any built-in ways to query an external database? Or does someone have a brief bit of code to get me started? Thanks.
  2. charac_1

    charac_1 New Member

    Joined:
    Aug 23, 2012
    Messages:
    19
    Never mind! I figured it out.
  3. skippybosco

    skippybosco CGI-Central Partner Staff Member

    Joined:
    Aug 22, 2006
    Messages:
    2,526
    How did you solve the issue in case others run into the same question?
  4. charac_1

    charac_1 New Member

    Joined:
    Aug 23, 2012
    Messages:
    19
    I have a WordPress page created for each of our members (who are artists). My function uses the title of the page (which is the member's name) to query the WordPress users table to get the member's username. Then the member's username is used to query the aMember database to retrieve the member's information so it can be displayed on their WordPress page. This function is in my WordPress functions.php file.

    function show_member() {
    global $wpdb;
    $usertitle = get_the_title();
    $aUsersID = $wpdb->get_col("SELECT $wpdb->users.ID FROM $wpdb->users WHERE display_name='" . $usertitle . "'");

    foreach ($aUsersID as $iUserID) :
    $user = get_userdata($iUserID);
    $userlogin = $user->user_login;

    $link = mysql_connect('aMember Pro database host name', 'db username', 'db password');
    if (!$link) { die('Could not connect: ' . mysql_error()); }
    mysql_select_db('db name');
    $query = sprintf("SELECT * FROM am_user WHERE login='%s'", mysql_real_escape_string($userlogin));
    $result = mysql_query($query);
    while ($row = mysql_fetch_assoc($result)) {
    echo "<strong>" . $row['name_f'] . " " . $row['name_l'] . "</strong>";
    if($row['business_name']) echo "<br />" . $row['business_name'];
    echo "<br />" . $row['street'];
    echo "<br />" . $row['city'] . ", " . $row['state'] . " " . $row['zip'];
    echo "<br />" . $row['phone'];
    echo "<br />" . $row['email'];
    echo "<br /><a href=" . $row['website'] . ">" . $row['website'] . "</a>";
    echo "<p>" . $row['biography'] , "</p>";
    }
    mysql_free_result($result);
    mysql_close($link);

    endforeach;
  5. skippybosco

    skippybosco CGI-Central Partner Staff Member

    Joined:
    Aug 22, 2006
    Messages:
    2,526
    Thanks for the follow up and sharing.

Share This Page