c连接mysql问题
最近在尝试CGI,我想把conn.c里的conn传到login_do.c用,报错Cannot connect to mysql server连续的一大窜,我在login_do.c里直接处理mysql操作不会出问题。是资源没有释放吗。conn.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "/usr/local/mysql/include/mysql.h"
5 MYSQL *connect(void)
6 {
7
8 MYSQL *conn;
9 char *server = "127.0.0.1";
10 char *user = "root";
11 char *password = "li.zhi.";
12 char *database = "blog";
13 conn = mysql_init(NULL);
14
15 if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0))
16 {
17 printf("%s\n",mysql_error(conn));
18 }
19 return conn;
20 }
login_do.c
1 #include<stdio.h>
2 #include "cgic.c"
3 #include <string.h>
4 #include <stdlib.h>
5 #include "blog.h"
6 #include <uuid/uuid.h>
7 #include "/usr/local/mysql/include/mysql.h"
8 extern MYSQL *connect(void);
9 int cgiMain()
10 {
11 MYSQL *conn;
12 char user_name[81],user_password[81],session_id[40];
13 uuid_t uuid;
14 if(cgiFormSubmitClicked("save") == cgiFormSuccess)
15 {
16
17 memset(session_id,0,sizeof(session_id));
18 uuid_generate(uuid);
19 uuid_unparse(uuid,session_id);
20 uuid_clear(uuid);
21
22 cgiFormString("user_name",user_name,81);
23 cgiHeaderCookieSetString("sess_id",session_id,0,"/","cgitest.localhost");
24 char sql[1024] = "insert into sessions(`sess_id`,`name`,`expire`) values('dd','administrator','')";
25 //freeConn();
26 cgiHeaderContentType("text/html");
27 printf("1");
28
29 conn = connect();
30
31 //if(mysql_query(conn,sql))
32 //{
33 // freeConn();
34 mysql_close(conn);
35 // fprintf(stderr,"%s\n",mysql_error(conn));
36 // exit(1);
37 //}
38 }
39 }