我想在C中執行MySQL查詢,但是在調用mysql_num_rows()時出現分段錯誤。C mysql_num_rows()分段錯誤
下面是我使用的代碼:
char *username = "[email protected]";
char *password = "pass";
char query[1000];
int len;
char *q = "SELECT * FROM Users WHERE `Email` = '%s' AND `Password` = '%s'";
len = snprintf(query, strlen(q) + strlen(username) + strlen(password), q, username, password);
MYSQL_RES *result;
if (db_query(query, result))
{
if (result != NULL)
{
int test_count = mysql_num_rows(result);
printf("%d\n", test_count);
}
}
else
{
printf("Query error\n");
}
這裏是db_query()函數:
bool db_query(const char *query, MYSQL_RES *result)
{
if (mysql_query(db_connection, query))
{
printf("mysql_query(): Error %u: %s\n", mysql_errno(db_connection), mysql_error(db_connection));
return false;
}
if (!(result = mysql_store_result(db_connection)))
{
printf("mysql_store_result(): Error %u: %s\n", mysql_errno(db_connection), mysql_error(db_connection));
return false;
}
return true;
}
我測試過的查詢和問題是不存在的連接也開始了。有任何想法嗎?
謝謝!
這樣做了,謝謝!看起來像我總是搞砸指針哈哈。乾杯! – user1667175 2013-05-05 12:31:45