23,120
社区成员
发帖
与我相关
我的任务
分享
int UserByEmail(char str1[],int client)
{
char buffer[500];
MYSQL db;/*connector*/
MYSQL_RES * result;/*result buffer*/
MYSQL_ROW row;/*one row of the result*/
int i;
/*初始化mysql*/
if(mysql_init(&db)==NULL)
{
fprintf(stderr,"Fail to initialize the db.\n");
return;
}
/*连接数据库*/
char host[32] = "localhost";
char user[32] = "mysql";
char passwd[38] = "passwd";
char dbc[32] = "mysql";
char getDate[1024];
char domain[2000];
char s[2000];
memset(getDate,0,1024);
if (NULL == (mysql_real_connect(&db,host,user,passwd,dbc,0,NULL,0)))
{
fprintf(stderr,"Connect to mysql Error:%s!",mysql_error(&db));
return;
}
char str[2000];
char str3[]="'";
memset(str,0,2000);
char Where_SQL[]="select * from users where email='";
strcat(str,Where_SQL);
strcat(str,str1);
strcat(str,str3);
printf("str=%s\n",str);
if(mysql_query(&db,str)!= 0)
{ // select();
fprintf(stderr,"Fail to query the db for information.\n");
return;
}
/*获得查询结果*/
result = mysql_store_result(&db);
if(result)
{
int num_fields;
num_fields = mysql_num_rows(result);
if(num_fields>0)
{
printf("get the result\n");
}
else
{
fprintf(stderr,"Fail to get the result.\n");
return;
}
}
else
{
fprintf(stderr,"Fail to get the result.\n");
return;
}
struct json_object *dal_data;
dal_data = json_object_new_object();
while((row=mysql_fetch_row(result)) != NULL)/*fetching each row*/
{
puts("================================================");
printf("id: %s\n",row[0]);
printf("email: %s\n",row[1]);
printf("password: %s\n",row[2]);
printf("status: %s\n",row[3]);
puts("================================================");
json_object_object_add(dal_data, "id", json_object_new_string(row[0]));
json_object_object_add(dal_data, "email", json_object_new_string(row[1]));
json_object_object_add(dal_data, "password", json_object_new_string(row[2]));
json_object_object_add(dal_data, "status", json_object_new_string(row[3]));
memset(buffer,0,500);
printf("\necho json_encode(dal_data);\n\n%s\n\n",json_object_to_json_string(dal_data));
strcat(buffer,json_object_to_json_string(dal_data));
strcat(buffer,"\n");
ssl_write(client,buffer);//这是个发送数据的函数调用
}
json_object_put(dal_data);
mysql_free_result(result);
mysql_close(&db);/////////////////////////////////////////////////////////////////////////////
}