【发布时间】:2014-11-16 02:10:34
【问题描述】:
我是 c 上 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 );
【问题讨论】:
-
以什么方式失败?请附上相关的错误信息。
-
检查
MYSQL_FIELD类型,并使用mysql_fetch_field,然后你可以做类似MYSQL_FIELD *field = mysql_fetch_field(result); printf("%s\n", field->name);API data structures are listed here的操作 -
我总是得到'1'的计数,使用当前代码。
-
字段是指列。你想计算行数。尝试调用
mysql_fetch_row- 如果你得到一行,那么用户就存在。