以下代码可以正常工作。
我想将MySQL凭证移至test.h
脚本
#include <stdio.h>
#include <string.h>
#include <fcgi_stdio.h>
#include <stdlib.h>
#include <mysql.h>
#include "test.h"
int main() {
while (FCGI_Accept() >= 0) {
char query[300];
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "";
char *database = "a";
conn = mysql_init(NULL);
if (mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
sprintf(query, "select * from table limit 1");
mysql_query(conn, query);
res = mysql_use_result(conn);
row = mysql_fetch_row(res);
}
printf("Content-type: text/html;\r\n");
printf("\r\n");
printf("%s", row[1]);
FCGI_Finish();
}
return 0;
}
好,让我们将所有这些复制并移动到test.h
测试
char query[300];
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "";
char *database = "a";
conn = mysql_init(NULL);
尝试编译script.c
In file included from test.c:7:
test.h:11: warning: data definition has no type or storage class
test.h:11: error: conflicting types for 'conn'
test.h:3: note: previous declaration of 'conn' was here
test.h:11: warning: initialization makes integer from pointer without a cast
test.h:11: error: initializer element is not constant
实际上还有更多错误。我只粘贴了前几行。
最佳答案
在头文件中使用#define
。
// test.h
#define SERVER "localhost"
#define PASSWORD ""
其余的保留在源代码文件中。
// script.c
char *server = SERVER;
char *user = USER;
关于c - 如何将MySQL凭证移至include.h?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44464772/