List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:June 27 2002 3:53am
Subject:Re: Getting field results using c
View as plain text  
At 10:42 -0500 6/26/02, rm wrote:
>I'm trying to retrieve a particular field using C API.  When I use this
>select statement from the mysql client it retrievs the correct result:
>
>mysql>SELECT ap_senders FROM mailtable WHERE pw_name='regis' and
>pw_domain='mbpost.com';
>
>But...
>
>In the C function listed below: 
>
>#include <stdio.h>
>#include <mysql.h>
>
>int main(char **args)
>{
>	MYSQL_RES *result;
>	MYSQL_FIELD *field;
>	MYSQL *connection, mysql:
>	int state;
>
>	mysql_init(&mysql);
>	connection = mysql_real_connect(&mysql,"localhost","root","",\
>	"mailtable",0,NULL,0);
>
>	if(connection == NULL) {
>		printf(mysql_error(&mysql));
>		return 1; }
>
>	state = mysql_query(connection, "SELECT ap_senders FROM\ mailtable
>WHERE pw_name='regis' and pw_domain='mbpost.com'");
>
>	if (state != 0) {
>		printf(mysql_error(connection));
>		retrun 1;}
>
>	result = mysql_store_result(connection);
>	printf("Found %d field\n", mysql_num_fields(result));
>
>	while ( ( field = mysql_fetch_field(result)) != NULL) {
>	printf("approved sender ID; %s\n", *field);  //problem here!
>
>	mysql_free_result(result);
>	mysql_close(connection);
>	printf(Done.\n");
>}
>
>It compiles without error or warning but instead of getting the results
>of what is in the field - I get the name of field.  The output is:
>
>Found 1 field
>approved sender ID; ap_senders
>Done.
>
>Of course ap_senders is the field name, not the query result.  So, I'm
>obviously asking for the wrong thing somewhere - any comments or
>suggestions are greatly appreciated.

mysql_fetch_field() fetches column metadata, not column data.
You want mysql_fetch_row() instead.  It's described in the manual.

>
>Regis

Thread
Getting field results using crm27 Jun
  • Re: Getting field results using cPaul DuBois27 Jun