2014-09-22 50 views
2

我是新來的c編程的MySQL編程。我需要檢查某個用戶是否存在於表中。我怎樣才能使用C API來做到這一點? 下面是我得到了什麼,但它不工作:MySQL的C API:檢查是否存在字段

if (mysql_query(con, "SELECT * FROM Users WHERE UserName='user3'")) 
    { 
     finish_with_error(con); 
    }  
    MYSQL_RES *result = mysql_store_result(con); 
    if (result == NULL) 
    { 
     finish_with_error(con); 
    } 
    int count = mysql_field_count(con); 
    printf("%d\n", count); 
+0

它以什麼方式失敗?請包含相關的錯誤消息。 – 2014-09-22 07:23:24

+1

檢查'MYSQL_FIELD'類型,並使用'mysql_fetch_field',然後你可以做類似'MYSQL_FIELD * field = mysql_fetch_field(result); printf(「%s \ n」,field-> name);'[API數據結構在這裏列出](http://dev.mysql.com/doc/refman/5.1/en/c-api-data-structures .html) – 2014-09-22 07:24:21

+0

我總是用當前代碼得到'1'的計數。 – Zaxter 2014-09-22 07:27:08

回答

2

任何有興趣,這是我在評論中寫道,終於結束了使用。

if (mysql_query(con, "SELECT field FROM table WHERE userName='user1'")) 
{ 
    finish_with_error(con); 
} 

MYSQL_RES *result = mysql_store_result(con); 

if (result == NULL) 
{ 
    finish_with_error(con); 
} 

MYSQL_ROW row; 
MYSQL_FIELD *field; 

if (row = mysql_fetch_row(result)) 
{ 
    printf("%s\n", row[0]); 
    db_pwd = row[0]; 
}