Thanks Paul. After looking a little more closely at the documentation I
figured this out. The example there looks like this:

num_fields = mysql_num_fields(result);
while((row = mysql_fetch_row(result)))
{
  unsigned long *lengths;
  lengths = mysql_fetch_lengths(result);
  for(i = 0; i < num_fields; i++)
  {
    printf("%.*s] ", (int) lengths[i], row[i] ? row[i] : "NULL");
  }
  printf("\n");
}

While this does not explicitely say that all columns are returned as
null-terminated strings, it is implied in the printf statement.

Thanks for you quick reply.

Andrew


> At 20:37 -0400 6/18/03, Andrew Pierce wrote:
>>I am trying to learn to use the C API for MySQL and have a question. I
>> have a table that has a primary key defined as int(11). I don't know
>> what data type this translates to in C. I thought maybe an unsigned int
>> or an unsigned long. I have this little loop that cycles through the
>> results of a query and displays the first two columns, the first being
>> the int(11) and the second a varchar. Here is the code:
>>
>>result = mysql_use_result(&mysql);
>>while((row = mysql_fetch_row(result))) {
>>   printf("%lu %s\n", row[0], row[1]);
>>}
>>
>>The first column is printed as garbage while the second column shows
>> the data correctly.
>
> INT in MySQL is a 4-byte data type, but that's on the server side. The
> result that you're observing is that all values are returned to the
> *client* as strings.  So you can either print the string using %s, or
> convert it to a C int and use %ld (you don't say that your MySQL type is
> UNSIGNED, so I'm assuming %ld rather than %lu).
>
> The MySQL 4.1 client/server protocol has some features that allow you to
> get back values in binary form without the conversion to string, but
> you're probably not using that.
>
>>
>>Help?
>>
>>Thanks.
>>
>>Andrew
>>
>>
>>
>>--
>>MySQL General Mailing List
>>For list archives: http://lists.mysql.com/mysql
>>To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]
>
>
> --
> Paul DuBois, Senior Technical Writer
> Madison, Wisconsin, USA
> MySQL AB, www.mysql.com
>
> Are you MySQL certified?  http://www.mysql.com/certification/




-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to