当前位置: 技术问答>linux和unix
mysql Segmentation fault的问题,求教
来源: 互联网 发布时间:2016-11-10
本文导语: 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) { ...
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=%sn",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 resultn");
}
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: %sn",row[0]);
printf("email: %sn",row[1]);
printf("password: %sn",row[2]);
printf("status: %sn",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);nn%snn",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);/////////////////////////////////////////////////////////////////////////////
}
上边是C-mysql一个api,程序在本机跑了好几天都没问题,但是拿到线上测试的时候发现程序跑到mysql_close(&db);(上边已经标出)这一句的时候就断了,错误提示:Segmentation fault
上线之后程序就改了个mysql的user和passwd,其他的都没改,本机跑没出现过问题
一上线就断了,把mysql_close(&db);这句注释掉之后程序就没Segmentation fault的错误提示了,但是mysql又不能不关;
我查过,网上说是 result初始化的问题,可是我初始化了,本机怎么就没问题?
个人觉得是不是线上环境和本机环境的差别?
向各位求教~~
|
|
帮楼主顶贴哦