由于某些原因,我的C代码不起作用。 SQL代码是正确的,当我运行它到phpmyadmin我得到我需要的结果,但如果我运行此代码它似乎是空的。现在有趣的部分是如果我删除
if((row = mysql_fetch_row(query_results)) !=0)
然后while循环工作,或者如果我删除
while((row = mysql_fetch_row(query_results)) !=0)
然后我就能得到结果。不知道为什么这两个人不能一起工作,但他们分别工作正常。
MYSQL_RES *query_results = mysql_store_result(conn);
if (query_results)
{ // make sure there *are* results..
MYSQL_ROW row;
if((row = mysql_fetch_row(query_results)) !=0)
{
while((row = mysql_fetch_row(query_results)) !=0)
{
printf("So far soo good 2\n");
/* Set a float 'f' to the value in 'row[0]', or
* 0.0f if it's NULL */
char *result_miner = row[0] ? row[0] : "NULL";
char *result_algorithm = row[1] ? row[1] : "NULL";
char *result_url = row[2] ? row[2] : "NULL";
int result_port = row[3] ? atof(row[3]) : 1;
char *result_username = row[4] ? row[4] : "NULL";
char *result_password = row[5] ? row[5] : "NULL";
strcpy(miner, result_miner);
strcpy(algorithm, result_algorithm);
strcpy(url, result_url);
strcpy(username, result_username);
strcpy(password, result_password);
port=result_port;
printf ("Miner Protocol is: %s\n", miner);
printf ("Algorithm is: %s\n", algorithm);
printf ("Mining URL is: %s\n", url);
printf ("Mining Port is: %d \n", port);
printf ("Username is: %s \n", username);
printf ("Password is: %s \n", password);
}
}else{
printf("Unable to locate active coin for hostname: %s", hostname);
exit (-1);
}
/* Free results when done */
mysql_free_result(query_results);
答案 0 :(得分:2)
基于reference page of mysql_fetch_row
,您必须这样做
这样:
while((row = mysql_fetch_row(query_results)))
{
printf("So far soo good 2\n");
char *result_miner = row[0] ? row[0] : "NULL";
char *result_algorithm = row[1] ? row[1] : "NULL";
char *result_url = row[2] ? row[2] : "NULL";
int result_port = row[3] ? atof(row[3]) : 1;
char *result_username = row[4] ? row[4] : "NULL";
char *result_password = row[5] ? row[5] : "NULL";
....
}
无需添加将while
添加到if((row = mysql_fetch_row(query_results)) !=0)
内,因为它会消耗一个mysql_fetch_row
排,如果你说你只期望一排,那么这就解释了你的原因
看不到任何价值观。
文档说如果不能获取更多行,NULL
将会
返回while
。你说你期待1行,但可能是你得到2行
其中一个有空值。在 my_ulonglong mysql_num_rows(MYSQL_RES *result)
循环之前,您可以使用
mysql_num_rows()
查看结果集中的实际行数。一世
怀疑你的查询结果是2行。
mysql_num_rows()
<强>描述强>
返回结果集中的行数。
mysql_store_result()
的使用取决于您是使用mysql_use_result()
还是mysql_store_result()
来返回结果集。 如果您使用mysql_num_rows()
,则可能会立即调用mysql_use_result()
。如果您使用mysql_num_rows()
,mysql_num_rows
在所有行之前都不会返回正确的值已检索到结果集。
修改强>
如何使用MYSQL_RES *query_results = mysql_store_result(conn);
if(query_results)
{
printf ("Number of rows: %lu\n",
(unsigned long) mysql_num_rows(query_results));
while((row = mysql_fetch_row(query_results)))
...
}
。
Swift