效果图:
主代码如下:
#include <stdlib.h> #include <stdio.h> #include "hiredis/hiredis.h" #include "mysql/mysql.h" #pragma comment(lib,"libmysql") #include <libmemcached/memcached.h> #include <string.h> #include <unistd.h> //gcc -g hello.c -o hello_c.cgi -I"D:\software\mysql-8.0.16-winx64\include" -L"D:\software\mysql-8.0.16-winx64\lib" -I"D:\software\Apache2.2\cgi-bin\libmemcached-win32\include" -L"D:\software\Apache2.2\cgi-bin" -llibmysql -lhiredis -lmemcached void commonInfo(char *argv[]) { printf("编译命令:gcc -g hello.c -o hello_c.cgi -I\"D:\\software\\mysql-8.0.16-winx64\\include\" -L\"D:\\software\\mysql-8.0.16-winx64\\lib\" -I\"D:\\software\\Apache2.2\\cgi-bin\\libmemcached-win32\\include\" -L\"D:\\software\\Apache2.2\\cgi-bin\" -llibmysql -lhiredis -lmemcached<br />\n"); printf("程序位置:%s<br />\n", argv[0]); printf("客户端信息1:%s<br />\n", getenv("HTTP_USER_AGENT")); } char *getparam(char *url, char *word) { char url1[256]; strcpy(url1, url); char * p1 = strtok(url1, "&"); while(p1 != NULL){ char urlpart1[256], urlpart2[256]; strcpy(urlpart1, p1); strcpy(urlpart2, p1); p1 = strtok(NULL, "&"); char * valstr = strstr(urlpart1, "="); valstr++; char * p2 = strrev(urlpart2); char * keystr1 = strstr(p2, "="); keystr1++; char * keystr = strrev(keystr1); if(strcmp(keystr, word) == 0){ return valstr; } } char * defaultvalue = ""; return defaultvalue; } void indexpage() { char * qs = getenv("QUERY_STRING"); char * actionkey = "action"; char * pagekey = "page"; char * pagesizekey = "pagesize"; char * defaultaction = "index"; char * actionval = getparam(qs, actionkey); char action[256]; strcpy(action, actionval); if(strcmp(action, "") == 0){ strcpy(action, defaultaction); } char * pageval = getparam(qs, pagekey); char pagestr[256]; strcpy(pagestr, pageval); int page = 0; if(strcmp(pagestr, "") == 0){ page = 1; } else{ page = atoi(pagestr); if(page < 1){ page = 1; } } char * pagesizeval = getparam(qs, pagesizekey); char pagesizestr[256]; strcpy(pagesizestr, pagesizeval); int pagesize = 0; if(strcmp(pagesizestr, "") == 0){ pagesize = 20; } else{ pagesize = atoi(pagesizestr); if(pagesize < 1){ pagesize = 20; } } //printf("indexpage::query_string: %s<br />\n", qs); //printf("indexpage::action: %s<br />\n", action); //printf("indexpage::page: %d<br />\n", page); //printf("indexpage::pagesize: %d<br />\n", pagesize); MYSQL *conn; MYSQL_RES *res; MYSQL_ROW row; char* server = "127.0.0.1";//本地连接 char* user = "root";// char* password = "123456";//mysql密码 char* database = "oa2_kings";//数据库名 conn = mysql_init(NULL); if(!mysql_real_connect(conn, server, user, password, database, 3306, NULL, 0)){ printf("Error connecting to database:%s\n",mysql_error(conn)); return ; } mysql_set_character_set(conn, "utf8mb4"); int start = (page - 1) * pagesize; char query[1024]; sprintf(query, "select id,staffName,staffNo,workEmail,workMobile from staff_baseinfo order by id desc limit %d,%d", start, pagesize);//需要查询的语句 int t,r; //printf("pagesize:%ds<br />\n", pagesize); //printf("%s\n", query); t = mysql_query(conn, query); res = mysql_use_result(conn); if(res){ printf("<table border=\"1\" bordercolor=\"#003366\" style=\"border-collapse:collapse;\">\n"); printf("<tr><td width=\"70px\">ID</td>\n<td width=\"120px\">姓名</td>\n<td width=\"120px\">员工号</td>\n<td width=\"190px\">邮件</td>\n<td width=\"100px\">手机</td>\n</tr>\n"); while((row = mysql_fetch_row(res)) != NULL){ printf("<tr>\n"); for(t = 0; t < mysql_num_fields(res); t++){ printf("<td>%8s</td>\n", row[t]); } printf("</tr>\n"); } printf("</table>\n\n"); } mysql_free_result(res); mysql_close(conn); char prepage[1024]; char netpage[1024]; sprintf(prepage, "<a href='?action=%s&page=%d&pagesize=%d'>上一页</a> ", action, page-1, pagesize); sprintf(netpage, "<a href='?action=%s&page=%d&pagesize=%d'>下一页</a> ", action, page+1, pagesize); printf(prepage); printf(netpage); printf("\n\n"); } void test1page() { printf("<h1>这是一个测试页面</h1><br />"); printf("<h2>这是一个测试页面</h2><br />"); printf("<h3>这是一个测试页面</h3><br />"); printf("<h4>这是一个测试页面</h4><br />"); } void test2page() { printf("<h4>这是一个打招呼页面</h4><br />"); printf("<h3>这是一个打招呼页面</h3><br />"); printf("<h2>这是一个打招呼页面</h2><br />"); printf("<h1>这是一个打招呼页面</h1><br />"); } //【Redis】Redis在Windows下的使用(hiredis+Qt5.7.0+mingw5.3.0) //https://blog.csdn.net/github_38647413/article/details/124106927 /** * cd hiredis * mkdir build * cd build * cmake .. * cmake --build . --target hiredis --config Release */ void redispage() { // 创建一个Redis连接上下文 redisContext *c = redisConnect("127.0.0.1", 6379); if (c == NULL) { printf("Can't allocate redis context"); return ; } if (c->err) { printf("Connection error: %s", c->errstr); return ; } // 执行PING命令 redisReply *reply = redisCommand(c, "PING"); printf("Redis-PING: %s<br />\n", reply->str); freeReplyObject(reply); reply = redisCommand(c, "SET key1 ceshi19820829"); printf("Redis-SET(key1): %s<br />\n", reply->str); freeReplyObject(reply); reply = redisCommand(c, "GET key1"); printf("Redis-GET(key1): %s<br />\n", reply->str); freeReplyObject(reply); // 断开连接 redisFree(c); } void memcachepage() { memcached_st *memc = NULL; memcached_return rc; memcached_server_st *server = NULL;// = memcached_servers_parse(NULL); time_t expiration = 3; uint32_t flags = 0; char *memcacheIp = "127.0.0.1"; char memcacheIpArr[256]; strcpy(memcacheIpArr, memcacheIp); int memcachePort1 = 11211; int memcachePort2 = 11212; //printf("IP地址: %s<br />\n", memcacheIp); //printf("端口: %d, %d<br />\n", memcachePort1, memcachePort2); char *key = "key2"; char *value = "ceshi19810909"; //char key[] = "key2"; //char value[] = "ceshi19810909"; size_t keylen = strlen(key); size_t valuelen = strlen(value); //printf("测试键: %s<br />\n", key); //printf("测试值: %s<br />\n", value); //printf("<br />\n"); memc = memcached_create(NULL); server = memcached_server_list_append(server, memcacheIpArr, memcachePort1, &rc); server = memcached_server_list_append(server, memcacheIpArr, memcachePort2, &rc); if(MEMCACHED_SUCCESS == rc){ //printf("memcached_server_list_append successfully<br />\n"); } else{ //printf("memcached_server_list_append failed[%s]<br />\n", memcached_strerror(memc, rc)); } //一致性哈希 //memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA, 1); //memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1); //memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1); //memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, 0); //// 设置连接超时为0,禁用连接超时 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1); rc = memcached_server_push(memc, server); if(MEMCACHED_SUCCESS == rc){ //printf("memcached_server_push successfully<br />\n"); } else{ //printf("memcached_server_push failed[%s]<br />\n", memcached_strerror(memc, rc)); } //printf("<br />\n"); //数据操作 rc = memcached_set(memc, key, keylen, value, valuelen, expiration, flags); if(MEMCACHED_SUCCESS == rc){ printf("Memcache-SET(%s): OK<br />\n", key); } else{ printf("Memcache-SET(%s): Failed[%s]<br />\n", key, memcached_strerror(memc, rc)); } char *result = memcached_get(memc, key, keylen, &valuelen, &flags, &rc); if(MEMCACHED_SUCCESS == rc){ printf("Memcache-GET(%s): %s<br />\n", key, result); } else{ printf("Memcache-GET(%s): Failed[%s]<br />\n", key, memcached_strerror(memc, rc)); } // 断开连接 memcached_server_list_free(server); memcached_free(memc); } void filepage() { char * qs = getenv("QUERY_STRING"); char * filekey = "file"; char * fileval = getparam(qs, filekey); char file[256]; strcpy(file, fileval); FILE *fp; char line[100]; char filename[256]; if(strcmp(file, "") == 0){ strcpy(filename, "./package.json"); } else{ sprintf(filename, "./%s", file); } fp = fopen(filename, "r"); printf("<b>文件名</b>:%s<br />\n", filename); printf("<b>文件内容</b>:<br />\n"); printf("<pre>\n"); while (fgets(line, sizeof(line), fp) != NULL) { printf("%s", line); } printf("</pre>\n"); fclose(fp); } //gcc -g hello.c -o hello_c.cgi -I"D:\software\mysql-8.0.16-winx64\include" -L"D:\software\mysql-8.0.16-winx64\lib" -llibmysql int main(int argc, char *argv[]) { printf("Content-type:text/html; charset=utf-8\r\n\r\n"); printf("<title>C语言,mysql</title>\n\n"); printf("<b>你好啊,屌毛</b><br /><br />\n\n"); commonInfo(argv); char * qs = getenv("QUERY_STRING"); char * actionkey = "action"; char * pagekey = "page"; char * pagesizekey = "pagesize"; char * defaultaction = "index"; char * actionval = getparam(qs, actionkey); char action[256]; strcpy(action, actionval); if(strcmp(action, "") == 0){ strcpy(action, defaultaction); } char * pageval = getparam(qs, pagekey); char pagestr[256]; strcpy(pagestr, pageval); int page = 0; if(strcmp(pagestr, "") == 0){ page = 1; } else{ page = atoi(pagestr); if(page < 1){ page = 1; } } char * pagesizeval = getparam(qs, pagesizekey); char pagesizestr[256]; strcpy(pagesizestr, pagesizeval); int pagesize = 0; if(strcmp(pagesizestr, "") == 0){ pagesize = 20; } else{ pagesize = atoi(pagesizestr); if(pagesize < 1){ pagesize = 20; } } //printf("main::query_string: %s<br />\n", qs); //printf("main::action: %s<br />\n", action); //printf("main::page: %d<br />\n", page); //printf("main::pagesize: %d<br />\n", pagesize); printf("页面:\n"); if(strcmp(action, "") == 0 || strcmp(action, "index") == 0){ printf("<a href='?action=index'><font color=red>首页1</font></a>\n"); } else{ printf("<a href='?action=index'><font>首页1</font></a>\n"); } if(strcmp(action, "test1") == 0){ printf("<a href='?action=test1'><font color=red>测试页1</font></a>\n"); } else{ printf("<a href='?action=test1'><font>测试页1</font></a>\n"); } if(strcmp(action, "test2") == 0){ printf("<a href='?action=test2'><font color=red>测试页2</font></a>\n"); } else{ printf("<a href='?action=test2'><font>测试页2</font></a>\n"); } if(strcmp(action, "redis") == 0){ printf("<a href='?action=redis'><font color=red>Redis</font></a>\n"); } else{ printf("<a href='?action=redis'><font>Redis</font></a>\n"); } if(strcmp(action, "memcache") == 0){ printf("<a href='?action=memcache'><font color=red>Memcache</font></a>\n"); } else{ printf("<a href='?action=memcache'><font>Memcache</font></a>\n"); } if(strcmp(action, "file") == 0){ printf("<a href='?action=file&file=go.sum'><font color=red>文件读取</font></a>\n"); } else{ printf("<a href='?action=file&file=go.sum'><font>文件读取</font></a>\n"); } printf("<a href='?action=test3'><font>不存在的页面</font></a>\n"); printf("<br /><br />\n\n"); if(strcmp(action, "") == 0 || strcmp(action, "index") == 0){ indexpage(); } else if(strcmp(action, "test1") == 0){ test1page(); } else if(strcmp(action, "test2") == 0){ test2page(); } else if(strcmp(action, "redis") == 0){ redispage(); } else if(strcmp(action, "memcache") == 0){ memcachepage(); } else if(strcmp(action, "file") == 0){ filepage(); } else{ printf("<h1>404, page not find.</h1>\n"); } return 0; }
相关lib:
libmemcached 需要自己用Visual Studio 2019 编译
标签:cgi,语言,pagesize,网站,char,printf,action,memcached,strcmp From: https://www.cnblogs.com/xuxiaobo/p/18129266