Hi
i wrote a program which get query from mysql and store in varible in C.but it can't convert Object mysql ro int.any idea??

#include <stdio.h>
#include <string.h>
#include <mysql.h>
#include <stdlib.h>

int main(){

char *email, name[30], domain[30], query[100], tmp[200];
int i=0,j,k;
unsigned int size;
email = "test@example.com";
char ch;

   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;

   char *server = "localhost";
   char *user = "root";
   char *password = "root"; /* set me first */
   char *database = "mydb";


do{
ch=email[i];
i++;
}while(ch != '@');

for(j=0;j<i-1;j++){
name[j]=email[j];
}
name[j]='\0';

for(k=0,++j;email[j];j++,k++){
domain[k]=email[j];
}

sprintf(query, "select size_file from mydbuser where username='%s' && ",name);
sprintf(tmp, "domains='%s'",domain);
strcat(query, tmp);


   conn = mysql_init(NULL);
   // Connect to database 

   if (!mysql_real_connect(conn, server,
         user, password, database, 0, NULL, 0)) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(1);
   }

   // send SQL query 

   if (mysql_query(conn, query)) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(1);
   }

   res = mysql_use_result(conn);

   // output table name
   //printf("MySQL Tables in mysql database:\n");
   while ((row = mysql_fetch_row(res)) != NULL)
        size=(int)row[0];
	printf("%s\n",row[0]);

   // close connection
   mysql_free_result(res);
   mysql_close(conn);


return 0;
}

which line of that code are you talking about?

According to the MySQL API Documentation, MYSQL_ROW isn't null-terminated strings. You should use mysql_fetch_lengths() to copy your desired field to a new string.

Mysql API Documentation

Example

unsigned long* lengths; 
int intval;
char buffer[20];

row = mysql_fetch_row (result); 
lengths = mysql_fetch_lengths (result); 
if (lengths[0] != 0) 
{ 
memcpy (buffer, row[0], lengths[0]); 
buffer[lengths[0]] = 0; 
sscanf (buffer, "%d", &intval); 
}

which line of that code are you talking about?

size=(int)row[0];

Thanks adatapost
my problem is solved like this ;)

if (lengths[0] != 0)
   {
    memcpy (buffer, row[0], lengths[0]);
    buffer[lengths[0]] = 0;
    databyte = strtoul(buffer,NULL,0);
   }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.