2016-10-03 61 views
0

我們被要求創建一個接受來自html的輸入的程序。我們涉足MySQL/MySQL網絡服務器,並試圖編譯這個程序。MySQL錯誤 - 由於錯誤,程序無法編譯

#include <stdio.h> 
    #include <string.h> 
    #include <mysql/mysql.h> 

    int processQueryResult(MYSQL *pConn, MYSQL_RES *pRes) 
    { 
    MYSQL_ROW  aRow; 
    unsigned int iCounter, iTotal; 

    aRow = mysql_fetch_row (pRes); 
    while (NULL != aRow) 
    { 
    /* Basic formatting for now */ 
    iTotal = mysql_num_fields (pRes); 
    for (iCounter = 0; iCounter < iTotal; iCounter++) 
    { 
     /* Check if empty data first before displaying */ 
     if (NULL != aRow[iCounter]) 
     { 
      printf ("%s\t", aRow[iCounter]); 
     } 
     else 
     { 
      printf ("NULL\t"); 
     } 
    } 
    printf ("\n"); 
    aRow = mysql_fetch_row (pRes); 
} 

/* Check if no error */ 
if (mysql_errno (pConn) == 0) 
{ 
    printf ("%lu rows returned\n\n", (unsigned long) mysql_num_rows (pRes)); 
} 

return 0; 
} 

int sendQuery(MYSQL *pConn, char *pCommand) 
{ 
MYSQL_RES *pRes; 

if (mysql_query (pConn, pCommand) != 0) /* the query failed */ 
{ 
    return 1; 
} 
pRes = mysql_store_result (pConn); 
if (NULL != pRes) /* a result set was returned */ 
{ 
    processQueryResult (pConn, pRes); 
    mysql_free_result (pRes); 

} 
else 
{ 
    if (mysql_field_count (pConn) == 0) 
    { 
     /* Modified table like INSERT command */ 
     printf ("%lu rows affected\n", (unsigned long) mysql_affected_rows(pConn)); 
    } 
    else /* an error occurred */ 
    { 
     return 1; 
    } 
} 

return 0; 
} 


int getSQLInput(char *pBuffer) 
{ 
int iCount = 0; 
int curData; 
do 
{ 
    curData = fgetc(stdin); 
    *pBuffer = curData; 
} while(*pBuffer == '\n'); 

do 
{ 
    curData = fgetc(stdin); 
    pBuffer[++iCount] = curData; 

} while ((curData != '\n') && (curData != EOF)); 

if (pBuffer[iCount-1] != ';') 
{ 
    pBuffer[iCount++]= ';'; 

} 
pBuffer[iCount]= '\0'; 

fputc('\n', stdout); 
return iCount; 
} 
int main(int argc, char **argv) 
{ 
MYSQL  *pConn; 
char  aCommand[1024]; 
char  aServer[1024]; 
char  aUser[1024]; 
char  aPassword[1024]; 
char  aDatabase[1024]; 

printf("Enter Server: "); 
scanf("%s", aServer); 

printf("Enter Database: "); 
scanf("%s", aDatabase); 

printf("Enter user: "); 
scanf("%s", aUser); 

printf("Enter password: "); 
scanf("%s", aPassword); 

pConn = mysql_init(NULL); 

/* Connect to database */ 
if (!mysql_real_connect(pConn, aServer, aUser, aPassword, aDatabase, 0, NULL, 0)) { 
    printf("Connect Error: %s\n", mysql_error(pConn)); 
    return 1; 
} 

printf("SQL command > "); 
getSQLInput(aCommand); 
while (strcmp(aCommand, "exit;") != 0) 
{ 
    if (0 != sendQuery(pConn, aCommand)) 
    { 
     printf("QUERY ERROR: %s\n", mysql_error(pConn)); 
    } 
    printf("SQL command > "); 
    getSQLInput(aCommand); 
} 

mysql_close(pConn); 
return 0; 
} 

當我嘗試編譯它,它顯示了錯誤的這個名單

[email protected]:~/workspace$ gcc basic.c -o basic.cgi 
/tmp/cct4DDaf.o: In function `processQueryResult': 
basic.c:(.text+0x18): undefined reference to `mysql_fetch_row' 
basic.c:(.text+0x2d): undefined reference to `mysql_num_fields' 
basic.c:(.text+0xad): undefined reference to `mysql_fetch_row' 
basic.c:(.text+0xc8): undefined reference to `mysql_errno' 
basic.c:(.text+0xd8): undefined reference to `mysql_num_rows' 
/tmp/cct4DDaf.o: In function `sendQuery': 
basic.c:(.text+0x114): undefined reference to `mysql_query' 
basic.c:(.text+0x12b): undefined reference to `mysql_store_result' 
basic.c:(.text+0x155): undefined reference to `mysql_free_result' 
basic.c:(.text+0x163): undefined reference to `mysql_field_count' 
basic.c:(.text+0x173): undefined reference to `mysql_affected_rows' 
/tmp/cct4DDaf.o: In function `main': 
basic.c:(.text+0x32c): undefined reference to `mysql_init' 
basic.c:(.text+0x378): undefined reference to `mysql_real_connect' 
basic.c:(.text+0x38c): undefined reference to `mysql_error' 
/tmp/ccwRjdCw.o: In function `processQueryResult': 
basic.c:(.text+0x18): undefined reference to `mysql_fetch_row' 
basic.c:(.text+0x2d): undefined reference to `mysql_num_fields' 
basic.c:(.text+0xad): undefined reference to `mysql_fetch_row' 
basic.c:(.text+0xc8): undefined reference to `mysql_errno' 
basic.c:(.text+0xd8): undefined reference to `mysql_num_rows' 
/tmp/ccwRjdCw.o: In function `sendQuery': 
basic.c:(.text+0x114): undefined reference to `mysql_query' 
basic.c:(.text+0x12b): undefined reference to `mysql_store_result' 
basic.c:(.text+0x155): undefined reference to `mysql_free_result' 
basic.c:(.text+0x163): undefined reference to `mysql_field_count' 
basic.c:(.text+0x173): undefined reference to `mysql_affected_rows' 
/tmp/ccwRjdCw.o: In function `main': 
basic.c:(.text+0x32c): undefined reference to `mysql_init' 
basic.c:(.text+0x378): undefined reference to `mysql_real_connect' 
basic.c:(.text+0x38c): undefined reference to `mysql_error' 
basic.c:(.text+0x3f4): undefined reference to `mysql_error' 
basic.c:(.text+0x44b): undefined reference to `mysql_close' 
collect2: error: ld returned 1 exit status 
+0

爲什麼你給我們留言3次?一次就夠了!您沒有指定要鏈接的MySQL庫 - 您是如何期望您的代碼能夠找到該庫的? –

回答

1

將MySQL庫鏈接到您的構建命令。例如:

gcc -o <&ltOutputFile>> <&ltSourceFileName>> `mysql_config --cflags --libs`

還要確保在build/linking命令的末尾添加mysql庫。鏈接庫的順序很重要。

這個link可能會有所幫助。