Accessing user data from php page

Discussion in 'aMember Pro v.4' started by rmara, Feb 16, 2013.

  1. rmara

    rmara Eager Beaver and Avid Learner

    Joined:
    Jan 21, 2013
    Messages:
    24
    New aMember user and could use a little help with our implementation. Attempting to load a php product page with member data that has access to the product. The page has access to all the members ID for the product page, but needs to grab each users data (name_f, name_l, custom fields, ...) from the aMember database. Looking for the php sql script including db access to obtain the data.


    Thank you in advance.
  2. thehpmc

    thehpmc Member

    Joined:
    Aug 24, 2006
    Messages:
    901
    Your php page:
    PHP:
    <?php
     
    require_once '/home/myusername/public_html/amember/library/Am/Lite.php';
     
    echo 
    Am_Lite::getInstance()->getName();  //First & last name
     
    echo Am_Lite::getInstance()->getEmail(); //email
    All in the wiki http://www.amember.com/docs/API/Lite
  3. rmara

    rmara Eager Beaver and Avid Learner

    Joined:
    Jan 21, 2013
    Messages:
    24
    Thank you. I am using this for the current logged in member. However, like to get the same information for each member who has access (via a product item) to the php page and list that member information on the php page.
  4. thehpmc

    thehpmc Member

    Joined:
    Aug 24, 2006
    Messages:
    901
    You just need to run a standard sql select statement on the page then if I understand correctly what you are wanting to do.
  5. rmara

    rmara Eager Beaver and Avid Learner

    Joined:
    Jan 21, 2013
    Messages:
    24
    Great. Can the page access the database through an aMember function call or would I just create a typical access routine?

    i.e.
    <?php
    $connection = mysql_connect ($host, $dbuser, $dbpass)
    or die ('Cannot connect to the Host.');

    $db = mysql_select_db("users", $connection)
    or die ('Can not connect to the database. '.mysql_errno());
    ?>
  6. thehpmc

    thehpmc Member

    Joined:
    Aug 24, 2006
    Messages:
    901
    Something along the lines of
    PHP:
    $sql =
    "SELECT
          *
    FROM
          am_user
    LEFT JOIN
          am_user_status ON user_id = user_id
    WHERE
          product_id = wanted product id"
    ;
     
    try {
      
    $db_conn = new PDO('mysql:host=localhost;dbname='.$db$user$pass);
    } catch (
    PDOException $e) {
      echo 
    "Could not connect to database";
      }
    // perform query
    $stmt $db_conn->query($sql);
    Note above has NOT been tested but is to give idea.

    Result set from query is held in $stmp

    You also need to '
    require_once '/home/myusername/public_html/amember/application/configs/config.php' and then read database login details from array there and insert them into above routine.
  7. rmara

    rmara Eager Beaver and Avid Learner

    Joined:
    Jan 21, 2013
    Messages:
    24
    I am new to PHP 5 and it is a lot different. After connecting to the database still unable to read any data with the code below. Does something stand out below?

    PHP:
            $sql ="SELECT * FROM am_user";
            
    $stmt $db_conn->prepare($sql);
            
    $stmt->execute();
            if (
    $results $stmt->fetch()) {
                print 
    "Results True";
                print 
    "DB User ID ".$results['user_id'];
            }
  8. thehpmc

    thehpmc Member

    Joined:
    Aug 24, 2006
    Messages:
    901
    Why do a 'prepare' and then 'execute' on next line? Do you get any error messages?

    Following on from my previous post
    PHP:
    $stmt $db_conn->query($sql);
    $rowCount=$stmt->rowCount();
    echo 
    $rowCount;
    while (
    $row $stmt->fetch()){
    echo 
    $row['login'] .'  '$row['name_f'] .'  '$row['name_l'] .' <br>';
    and what do you get?
  9. rmara

    rmara Eager Beaver and Avid Learner

    Joined:
    Jan 21, 2013
    Messages:
    24
    No errors on my previous code or on the connect. The error below occurs when getting the count.

    "Fatal error: Call to a member function rowCount() on a non-object"

    Here is the connect which is in a try and no errors and part of a include at the top of the file.

    $db_conn = new PDO ('mysql:host=localhost;'.$db, $user, $pass);

    I must be missing something.
  10. rmara

    rmara Eager Beaver and Avid Learner

    Joined:
    Jan 21, 2013
    Messages:
    24
    I was missing something! When all else fails jump into phpMyAdmin to check out the query for errors. Your assistance was greatly appreciated and very helpful. Have a great week and let me know if I can ever assist you.
  11. thehpmc

    thehpmc Member

    Joined:
    Aug 24, 2006
    Messages:
    901
    Glad you got it sorted.

    The error message you ended up getting is what I would expect if the query failed for some reason.

    Even if no database entries were found matching search criteria the count function would still return a value, in this case 0.

Share This Page