Professional Web Applications Themes

anyway to return more than one value? - PHP Development

Hi list. Ok I know it's not possible to "return" more than one value. But I'm going to explain what I'd like to do so maybe there's an easy way to do it. I've got some functions that query a database and turn the result into an array and return that array. What I'd like to do is not only return the array of results but ALSO return a row count and field count of that result. Here is some pseudo code: function get_results() { // query database $result = query($sql); // turn result set into a useable array $theResultArray ...

  1. #1

    Default anyway to return more than one value?

    Hi list.

    Ok I know it's not possible to "return" more than one value. But I'm
    going to explain what I'd like to do so maybe there's an easy way to do
    it.

    I've got some functions that query a database and turn the result into
    an array and return that array. What I'd like to do is not only return
    the array of results but ALSO return a row count and field count of that
    result.

    Here is some pseudo code:

    function get_results()
    {
    // query database
    $result = query($sql);

    // turn result set into a useable array
    $theResultArray = get_results($result);

    $rows = mysql_num_rows($result);
    $fields = mysql_num_fields($result);

    return $theResultArray;
    return $rows;
    return $fields;
    }

    Ok I know that won't work but that's just basically what I want to do.

    The only way around this I've come up with is to stick all the values
    into ANOTHER array and return then and then dissect that array in the
    calling function, but that just seems messy.

    All of a sudden the word "reference" came to mind. Is that what I want
    to use?


    Any help would be appreciated.


    Thanks,
    Chris.
    --
    Don't like reformatting your Outlook replies? Now there's relief!
    http://home.in.tum.de/~jain/software/outlook-quotefix/
    Chris Guest

  2. #2

    Default Re: [PHP] anyway to return more than one value?

    On Wed, 2003-11-05 at 19:02, Chris W. Parker wrote: 

    You can do the following:

    function get_results( &$rows, &$fields )
    {
    // query database
    $result = query($sql);

    // turn result set into a useable array
    $theResultArray = get_results($result);

    $rows = mysql_num_rows($result);
    $fields = mysql_num_fields($result);

    return $theResultArray;
    }

    HTH,
    Rob.
    --
    ..------------------------------------------------------------.
    | InterJinn Application Framework - http://www.interjinn.com |
    :------------------------------------------------------------:
    | An application and templating framework for PHP. Boasting |
    | a powerful, scalable system for accessing system services |
    | such as forms, properties, sessions, and caches. InterJinn |
    | also provides an extremely flexible architecture for |
    | creating re-usable components quickly and easily. |
    `------------------------------------------------------------'
    Robert Guest

  3. #3

    Default Re: [PHP] anyway to return more than one value?

    Chris W. Parker wrote: 

    Are you talking about something like this:

    $retval['result'] = get_results($result);
    $retval['rows'] = mysql_num_rows($result);
    $retval['fields'] = mysql_num_fields($result);

    return $retval;

    --
    ---John Holmes...

    Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

    php|architect: The Magazine for PHP Professionals www.phparch.com
    John Guest

  4. #4

    Default Re: [PHP] anyway to return more than one value?


    One possible solution could be to write a simple class that will hold
    all data you need at one single place:

    class MyData {
    var $_resultArray;
    var $_rows;
    var $_fields;

    function MyData($resultArray, $rows, $fields) {
    $this->data = $data;
    $this->rowcounr = $rowcount;
    $this->fildcount = $fildcount;
    }

    function getResultArray() {
    return $this->_resultArray;
    }

    function getRows() {
    return $this->_rows;
    }

    function getFilds () {
    return $this->_fields;
    }
    }

    To put some data in that object write:

    $data = new MyData($theResultArray, $rows, $fields);

    To get back your stuff in your code use something like:

    $theResultArray = MyData->getResultArray();
    $rows = MyData->getRows();
    $fields = MyData->getFields();

    This is only an example code, you should adjust it according your needs

    hth

    Boyan
    --


    Chris W. Parker wrote:
     
    Boyan Guest

  5. #5

    Default Re: [PHP] anyway to return more than one value?


    [snip]
     

    [/snip]

    ... if you spend some time moving closer to the object oriented
    approach (php classes in this case) you may find that the development
    could be much more clear and easy ... and then will never come back to
    the functional/procedural way of programming

    --

    Chris W. Parker wrote:
     
    Boyan Guest

Similar Threads

  1. return SQL ID
    By aredovian in forum Macromedia ColdFusion
    Replies: 0
    Last Post: March 18th, 06:44 PM
  2. return xml
    By Poppy in forum ASP.NET Web Services
    Replies: 3
    Last Post: April 27th, 04:01 PM
  3. goToNetPage, return or not return
    By GONS in forum Macromedia Director Basics
    Replies: 1
    Last Post: January 16th, 08:44 PM
  4. Return Key
    By Ethan in forum Mac Portable
    Replies: 0
    Last Post: July 14th, 11:43 PM
  5. Return to Tab
    By Joe Fallon in forum Microsoft Access
    Replies: 0
    Last Post: July 12th, 04:53 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139