Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2002
    Posts
    86
    Thanks
    2
    Thanked 1 Time in 1 Post

    Rebuild this function

    Hi guys,

    I have this function below which works until I there are more than 1 result pulled from the second inside query.

    I need to modify it so it loops through the results passed from the middle query, outputting an array of results each time.

    Bit stuck on this bit though so need some help ?

    PHP Code:
    public function getDownloads($product_id) {
            
            
    $sql "SELECT * FROM " DB_PREFIX "download d LEFT JOIN " DB_PREFIX "download_description dd ON (d.download_id = dd.download_id)" "WHERE d.download_id = (SELECT download_id FROM " DB_PREFIX "product_to_download WHERE product_id = '$product_id')";
            
            
    $query $this->db->query($sql);
            return 
    $query->rows;    
        } 

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    You are obviously using some kind of custom database class so without knowing what this is doing it would be difficult to help

    PHP Code:
    $query $this->db->query($sql); 
    And what do you expect to return from your function? A raw result set from the db or do you plan to massage the resulting data into another structure before returning it?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New Coder
    Join Date
    Aug 2002
    Posts
    86
    Thanks
    2
    Thanked 1 Time in 1 Post
    Hi Spookster,

    Sorry for the long delay in replying lol

    This is the custom class function

    PHP Code:
          public function query($sql) {
            
    $resource mysql_query($sql$this->connection);

            if (
    $resource) {
                if (
    is_resource($resource)) {
                    
    $i 0;
            
                    
    $data = array();
            
                    while (
    $result mysql_fetch_assoc($resource)) {
                        
    $data[$i] = $result;
            
                        
    $i++;
                    }
                    
                    
    mysql_free_result($resource);
                    
                    
    $query = new stdClass();
                    
    $query->row = isset($data[0]) ? $data[0] : array();
                    
    $query->rows $data;
                    
    $query->num_rows $i;
                    
                    unset(
    $data);

                    return 
    $query;    
                } else {
                    return 
    true;
                }
            } else {
                
    trigger_error('Error: ' mysql_error($this->connection) . '<br />Error No: ' mysql_errno($this->connection) . '<br />' $sql);
                exit();
            }
          } 

  • #4
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think this is some codes from opencart.

    Im not really sure what you are up to, but based from the function name.
    d.download_id = (middle query) will output a single row even though the middle query has more than 1 result

    d.download_id IN (middle query) to get all the downloads for a product id

    PHP Code:
    public function getDownloads($product_id) {
            
            
    $sql "SELECT * FROM " DB_PREFIX "download d LEFT JOIN " DB_PREFIX "download_description dd ON (d.download_id = dd.download_id)" "WHERE d.download_id IN (SELECT download_id FROM " DB_PREFIX "product_to_download WHERE product_id = '$product_id')";
            
            
    $query $this->db->query($sql);
            return 
    $query->rows;    
        } 

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,281
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    Quote Originally Posted by waps2 View Post
    Hi Spookster,

    Sorry for the long delay in replying lol

    This is the custom class function
    So as spookster was saying, what are you wanting to do with the data you get back?

    You're not helping yourself at all here.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

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