mysql Segmentation fault的问题,求教

leihelin5201314 2011-02-25 09:32:06
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);/////////////////////////////////////////////////////////////////////////////
}

上边是C-mysql一个api,程序在本机跑了好几天都没问题,但是拿到线上测试的时候发现程序跑到mysql_close(&db);(上边已经标出)这一句的时候就断了,错误提示:Segmentation fault
上线之后程序就改了个mysql的user和passwd,其他的都没改,本机跑没出现过问题
一上线就断了,把mysql_close(&db);这句注释掉之后程序就没Segmentation fault的错误提示了,但是mysql又不能不关;
我查过,网上说是 result初始化的问题,可是我初始化了,本机怎么就没问题?
个人觉得是不是线上环境和本机环境的差别?
向各位求教~~
...全文
478 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
莫_问 2011-02-28
  • 打赏
  • 举报
回复
查下官方文档,应该是环境差异,看下操作系统,以及mysql版本是否一致。
你妹的特盗不 2011-02-28
  • 打赏
  • 举报
回复
帮楼主顶贴哦
唤水痕 2011-02-28
  • 打赏
  • 举报
回复
leihelin5201314 2011-02-25
  • 打赏
  • 举报
回复
人呢?
leihelin5201314 2011-02-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hhwei1985 的回复:]

sf.
[/Quote]
你···

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧