标题:c连接mysql问题
只看楼主
lizhiddlz
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-4-4
 问题点数:0 回复次数:0 
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 }
搜索更多相关主题的帖子: password database include server mysql 
2013-06-25 13:26



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-416008-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040519 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved