Reputation: 9
bool MySql_Register(const char* id, const char* pw) {
MYSQL* connect_ptr;
connect_ptr = mysql_init(NULL);
if(!mysql_real_connect(connect_ptr, HOST, USER, PW, NAME, 3306, NULL, 0)) {
fprintf(stderr, "%s ",mysql_error(connect_ptr));
exit(1);
}
char sql[256] = {0};
sprintf(sql, "select * from user where id='%s'", id);
mysql_query(connect_ptr, sql);
int len = mysql_affected_rows(connect_ptr);
if(len == 1)
return PK_ID_OVERLAP;
sprintf(sql, "insert into user (id, pw) values ('%s', '%s')", id, pw);
mysql_query(connect_ptr, sql);
len = mysql_affected_rows(connect_ptr);
if(len == 1)
return true;
else
return false;
}
mysql_connect
and mysql_query
running is very well.
but mysql_affected_rows
returned value -1.
I don't know where is the problem....
Upvotes: 0
Views: 50
Reputation: 20746
From the documentation:
-1 indicates that the query returned an error or that, for a SELECT query, mysql_affected_rows() was called prior to calling mysql_store_result()
Moreover, I suggest you to use prepared statements instead of sprintf
to avoid SQL injections.
Upvotes: 1