IT may seem loopy to you but how do you think that PHP does it?  Somewhere 
in the PHP record handling code is a loop that reads all of the field 
names. Now, it might cache those names into something like an array, a 
linked list, or a hash map so that looking them up again doesn't require a 
full scan of the field names again but there was at least one loop in 
order for it to build its column-name/index association.

What you can do is to build your own hash map (hashes provide fast 
look-ups for exact matches, like column names) and when you get your first 
response, you populate your hash map. Then later you pass to a retrieval 
function (that you write) pointers to your map, the field name you want to 
get,  and the data and it would spit out the field value you requested.

When you are coding at the C level, you have actually gotten so close to 
the database that some "helpful" features that you may be used to (like 
name/field associations) with higher-level languages (like PHP) are 
"above" you. If you like that functionality, you have to write your own 
routine to provide it.

Shawn Green
Database Administrator
Unimin Corporation - Spruce Pine

"Matthew Boehm" <[EMAIL PROTECTED]> wrote on 10/11/2004 10:50:32 AM:

> What is the best way to access a specific field in C? Its really easy in
> PHP...
> 
> PHP
> -------
> $res = mysql_real_query($mysql,"SELECT col1, col2 FROM table");
> while($row = mysql_fetch_row($res)) {
>     print $row['col1'];
>     print $row['col2'];
> }
> 
> Is the only way/best way to do the above in C by using a nested 
for-loop?
> Ex:
> 
> fields = mysql_fetch_fields(res);
> while((row=mysql_fetch_row(res)) {
>     for(x=0;x<numFields;x++) {
>         sprintf(output, "Column name: %s  Column Value: %s\n",
> fields[x].name, row[x]);
>     }
> }
> 
> Seems painful and extra-loopy to do it in C. Is there a better way?
> 
> Thanks,
> Matthew
> 
> 
> -- 
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]
> 

Reply via email to