Fork me on GitHub

Getting query result in an array (unknown #fields)  Bottom

Go to page 1 - 2 [+1]:

  • I'm stuck :(

    After spending a lot of hours trying to figure this out, i'm seeking some help.
    I have a query which produces a result with a variable number of fields. I need to get the results in an array (preferrably fieldname->value), so i can assign them to a template.

    The PHP manual, gave some pointers but even with the functions mysql_num_fields, mysql_field_name and their comments i can't figure it out.

    Any idea's or suggestions on where to look ?
  • Ok, i'm starting to get somewhere.

    The following code gets all values in an error. Now i just have to get the columnnames in there to.

    Code

    $result = $dbconn->Execute($sql);

        // Check for an error with the database code, and if so set an appropriate
        // error message and return
        if ($dbconn->ErrorNo() != 0) {
            pnSessionSetVar('errormsg', _GETITEMSFAILED);
            return false;
        }

    $items=array();
    while (list($key,$value) = each($result->fields))
          {     // echo 'colname = ' . mysql_field_name($result,$key
                $items[$key]=$value;
          }


    Now i just have to figure but why mysql_field_name($result,$key) doesn't return anything. If i put an echo statement in the while
  • Very sorry if my skepticism is not well placed but...

    tycho


    ...which produces a result with a variable number of fields...


    Seems to me to be a strange way to do things... wouldn't it be better that you know in advance what will be the output result ?
    Even if you have to do something like different where conditions based on parameters, etc, etc...

    See Example module (pnuserapi.php) for the array thing...
  • Firstly don't drop back to using mysql* functions. This will break the DB independance offered by adodb. I suggest that you review the adodb docs - the GetRowAssoc method would seem to do what you want.

    -Mark

    --
    Visit My homepage and Zikula themes.
  • Thanks for you're answer Chestnut.

    Skeptisicm is always welcomed!
    I'm trying to convert an old module to a pnRender based module. The module allows the user to setup as many custom fields as the want, and modifies the main table to add those custom fields (c_field_1 to c_field_x) to it.
    I could count the custom fields in advance with a simple count query, and that would give me the fieldnames to. But then i get in problems with the list() thing because you can't embed a for loop in the list() function.

    So something like :

    Code

    list( $known1,
          $known2,
          $known3
          for ($i=0;$i<$numberofcustomfields;i++){
             , $custom_$i
          } = array(blah)

    doesn't work.

    The example pnuserapi assumes the number of fields, and there names are known.
  • markwest

    Firstly don't drop back to using mysql* functions. This will break the DB independance offered by adodb. I suggest that you review the adodb docs - the GetRowAssoc method would seem to do what you want.

    -Mark


    Thanks got it working now

    Code

    $items=array();
    while (!$result->EOF) {
            $items[]=$result->GetRowAssoc();   
            $result->MoveNext();
        }


    About the falling back, first things first :D
  • Try this as it should produce the same result and be even simpler... Replace the four lines of code you've posted with

    Code

    $items = $result->GetAssoc();


    Reference: GetAssoc method.

    -Mark

    --
    Visit My homepage and Zikula themes.
  • At a first glance it then loses the columnnames. But i'll try a bit more later. Thanks.
  • I've not tried this myself so you maybe right... The downside with this approach is that your not then able to peform permissions checks on the result set (as is done in the example module) but if permissions are a requirement (and if it works as I think it should) it's certainly cleaner code.

    -Mark

    --
    Visit My homepage and Zikula themes.
  • mmm

    Code

    $items[] = $result->GetAssoc();


    ? :)
  • My mistake... Of course the code should be

    Code

    $items = $result->GetAssoc();


    -Mark

    --
    Visit My homepage and Zikula themes.
  • markwest

    My mistake... Of course the code should be

    Code

    $items = $result->GetAssoc();


    -Mark


    I'm lost icon_lol
  • Franky,

    The GetAssoc method of an ADODB recordset object will automatically generate an associative array from the recordset. If your not wanting to apply permissions to the items is a recordset this method will mean you don't have to loop through the recordset generating the associative array yourself.

    As I say i've not used it myself but this is what the docs suggest the result would be.....

    -Mark

    --
    Visit My homepage and Zikula themes.
  • No I know that...

    But I must probably reads the doc anyway, that is for sure...

    My lost was about the [] for the items = ... :)

    EDIT: Oups... my bad...
  • Code

    $items=array();
    while (!$result->EOF) {
            $items[]=$result->GetRowAssoc();   
            $result->MoveNext();
        }

    Can somebody explain what exactly does the MoveNext() function ?

    Thanks a lot!

Go to page 1 - 2 [+1]:

  • 0 users

This list is based on users active over the last 60 minutes.