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 ?
Watch
GitHub Core
Show your support for Zikula! Sign up at Github account and watch the Core project!
GitHub Modules
- mesteele101 responded to »ERR (3): E_USER_ERROR: Smarty error: [in pagesvar:pagesitem2en line XXX]…« 07:01 AM
- mazdev responded to »Pages 2.5.0 and updating - Page not found« 06:41 AM
- ehdwma created topic »Hide "Register new account" and change template to 3 col« 06:27 AM
- mesteele101 responded to »Zikula 1.3.3 - Selecting a category in Pages not working« 01:29 AM
- mdee created topic »How to implement returnpage ?« 01:00 AM
- nestormateo responded to »Fillters in Clip« 24. May
- damon responded to »Can the Updated Version Check be Turned Off (Z 1.3)« 24. May
Zikula Blog
- Anatomy of Open Source Projects on Mar 07
- Continuous Review on Mar 01
- Not Invented Here on Feb 24
- How to Contribute Your Code at Github on Jan 13
- 10 Steps to Coding-Nirvana: Tips for Successful Module Writing on Nov 12
- Submitting Bug Report Tickets That Get Results on Aug 17
- Cozi Tricks #1: Syntax Highlighting on Aug 07
Login
Getting query result in an array (unknown #fields)
-
**unknown user**
- Rank: Softmore
- Registered: Mar 16, 2002
- Last visit: Oct 21, 2009
- Posts: 171
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 -
**unknown user**
- Registered: Mar 16, 2002
- Last visit: Oct 21, 2009
- Posts: 1097
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... -
- Rank: Team Member
- Registered: Mar 18, 2002
- Last visit: Oct 21, 2009
- Posts: 6606
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. -
**unknown user**
- Rank: Softmore
- Registered: Mar 16, 2002
- Last visit: Oct 21, 2009
- Posts: 171
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
doesn't work.
The example pnuserapi assumes the number of fields, and there names are known. -
**unknown user**
- Rank: Softmore
- Registered: Mar 16, 2002
- Last visit: Oct 21, 2009
- Posts: 171
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
About the falling back, first things first :D -
- Rank: Team Member
- Registered: Mar 18, 2002
- Last visit: Oct 21, 2009
- Posts: 6606
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. -
- Rank: Team Member
- Registered: Mar 18, 2002
- Last visit: Oct 21, 2009
- Posts: 6606
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. -
- Rank: Team Member
- Registered: Mar 18, 2002
- Last visit: Oct 21, 2009
- Posts: 6606
My mistake... Of course the code should be
Code
$items = $result->GetAssoc();
-Mark
--
Visit My homepage and Zikula themes. -
- Rank: Team Member
- Registered: Mar 18, 2002
- Last visit: Oct 21, 2009
- Posts: 6606
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.
- Moderated by:
- Support
Users on-line
- 0 users
This list is based on users active over the last 60 minutes.
