我想从我的数据库 (MySQL) 中获取结果并将它们存储在一个数组中。我尝试了一些事情,但我没有解决办法。
mysql_real_query(mysql, query, strlen(query));
mysql_res = mysql_store_result(mysql);
rows_number = (unsigned long) mysql_num_rows (mysql_res);
printf ("Number of Rows: %lu\n\n", rows_number);
while ((row = mysql_fetch_row (mysql_res)) != NULL) {
for (i = 0; i < mysql_num_fields(mysql_res); i ++)
printf ("%s ",row[i]); //this is printed like it should be.
printf("\n");
char *val = row[i]; //should save it to an char
printf("%s",val);
但是如果我启动程序,输出是:(仅提取!) 这是 printf ("%s ",row[i]); 发送的内容:
1 1 1 0006
这是打印出来的 char val:
</alias>
<alias>windows-1256</alias>
<collation name="cp1256_bin" id="67" order="Binary" flag="binary"/>
<collation name="cp1256_general_ci" id="57" order="Arabic" flag="primary">
<order>Arabic</order>
<order>Persian</order>
<order>Pakistani</order>
<order>Urdu</order>
</collation>
</charset>
那么有什么问题呢? 感谢您的帮助!!
最佳答案
i
正在 for 循环的边界之外使用:
代替:
for (i = 0; i < mysql_num_fields(mysql_res); i ++)
printf ("%s ",row[i]); //this is printed like it should be.
你可能是这个意思:
for (i = 0; i < mysql_num_fields(mysql_res); i ++)
{
printf ("%s ",row[i]); //this is printed like it should be.
printf("\n");
char *val = row[i]; //should save it to an char
printf("%s",val);
}
关于mysql - C - 获取 MySQL 行到字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22477824/