2014-09-22 51 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]; 
}