SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    phplib mysql class question

    Been using this for a while but want to do some things a bit differently. At the moment i do something like

    PHP Code:
    $query "SELECT * from email WHERE name='admin'";
    $db->query($query);
    while(
    $db->next_record()) {
          
    $email_from_admin $db->f("email");

    but what i really want is to be able to do a specific query and get the results back in the form of an associative array, so doing something like
    PHP Code:
    $temp $db->query($query)
    $user $db->next_record($temp
    sort of thing where i can then use $user[email] sort of thing. Is this possible?

    The other problem i am having is that i do an SQL query which requires a loop through returned results, and sometimes i have to do another sql query inside that loop (to get a specific template - only once it is then stored in a cache) but this mucks up the loop because the query is changed.

    Am i missing something here?

    thanks

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For your first problem, remember a while back when I had you adjust the next_record() function in the mysql class? Well thats where this comes into play, first make sure that your next_record() function looks like this:

    PHP Code:
      /* public: walk result set */
      
    function next_record($stype MYSQL_BOTH) {
        if (!
    $this->Query_ID) {
          
    $this->halt("next_record called with no query pending.");
          return 
    0;
        }

        
    $this->Record = @mysql_fetch_array($this->Query_ID$stype);
        
    $this->Row   += 1;
        
    $this->Errno  mysql_errno();
        
    $this->Error  mysql_error();

        
    $stat is_array($this->Record);
        if (!
    $stat && $this->Auto_Free) {
          
    $this->free();
        }
        return 
    $stat;
      } 

    Now in your script you can simply do this:


    PHP Code:
    $query "SELECT * from email WHERE name='admin'";
    $db->query($query);

    #Specify to the phplib class to return the next_record 
    #in the form of an associative array
    $db->next_record(MYSQL_ASSOC);

    #Assign the current record to the var $user in the form of 
    #an associative array
    $user $db->Record;

    #prints an element form the array
    print $user[email]; 

    For you second question, just create two db objects like this

    PHP Code:
    $db = new DB;
    $db2 = new DB
    Then in your loop for the nested query use the $db2 object and use $db2->free() after each nested query. That way your original query will stay in memeory and you can use the $db2 object for nested queries and free it up after each loop of the original query if that makes sense. Let me know if that works for you.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol, i was just coming back to the forums with that bit you gave me. Just found it and wondered if that was part of the answer (after reading a bit harder through my notes) but thought it a bit weird it is not standard part of phplib.

    But yes, solved the problem perfectly. Just tried combining them and this works a tread

    PHP Code:
    while ($article $db->row_array()) { 
    where row_array is what you gave me previously:

    PHP Code:
    # added by freddydoesphp
    /* public: get arrays for each record */
      
    function row_array() {
        if (!
    $this->Query_ID) {
          
    $this->halt("next_record called with no query pending.");
          return 
    0;
        }

        
    $this->Record = @mysql_fetch_assoc($this->Query_ID);
        
    $this->Row   += 1;
        
    $this->Errno  mysql_errno();
        
    $this->Error  mysql_error();

        
    $stat is_array($this->Record);
        if (!
    $stat && $this->Auto_Free) {
          
    $this->free();
          return 
    false;
        }
        else {
          return 
    $this->Record;
        }
      } 
    it is a bit different to what you posted below though, what is the difference or does it not matter?

    Off to try the $db and $db2 thing, at moment caching templates before hand but can't rely on that.

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LOL I had forgotten about the extra function, I think maybe I scrapped that idea once I figured out how to combine this technique into the existing next_record() function. I don't think there is any difference between the two except for the next_record() adjustment makes life a little more modular so to speak, where next_record() becomes a more versatile tool.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •