在MYSQL C API中处理SELECT查询结果的问题 [英] Problem with handling the result of SELECT query in MYSQL C API
问题描述
我的一个脚本出现内部服务器错误.我正在使用MYSQL C API. https://dev.mysql.com/doc/refman/5.6/en/c-api.html
I'm getting a Internal Server Error with one of my scripts. I'm using MYSQL C API. https://dev.mysql.com/doc/refman/5.6/en/c-api.html
这是我脚本的相应部分:
Here is the corresponding part of my script:
MYSQL *con;
MYSQL_RES *result;
MYSQL_ROW robe;
con = mysql_init(NULL);
if (!mysql_real_connect(valid values)) {
printf("Content-type: text/html\n\n");
printf("Could not connect\n");
exit(0); }
char somequery[512];
//userinput is sanitized beforehand
int sog = sprintf(somequery, "SELECT password from testtab WHERE username='%s'", userinput);
if (sog < 0) {
printf("Content-type: text/html\n\n");
printf("Something went wrong with Sprintf\n");
exit(0); }
int bos = mysql_real_query(con, somequery, strlen(somequery));
if (bos != 0) {
printf("Content-type: text/html\n\n");
printf("The query produced no result\n");
exit(0); }
result = mysql_store_result(con);
if (result == NULL) {
printf("Content-type: text/html\n\n");
printf("No Result Set Produced\n");
exit(0); }
robe = mysql_fetch_row(result);
char *passdb = robe[0];
printf("Content-type: text/html\n\n");
printf("And it is: %s", passdb);
HTML表单通过POST提交到此脚本(上面显示了其中的一部分).预先提交数据库中存在的用户名时,我没有收到任何错误.一切正常.
A HTML form submits via POST to this script (part of which is seen above). When I submit a username which exists in the database beforehand, I'm receiving no error. Everything works fine.
当我提交的用户名在所述表(testtab)中不存在时,就会出现问题.好吧,我收到500内部服务器错误.我也查看了Apache错误日志:在标头之前输出脚本的结尾".
The problem arises, when I'm submitting a username that doesn't exist in the said table(testtab). Well, I'm getting 500 Internal Server Error. I have looked at Apache Error log as well: "End of Script output before Headers".
到目前为止,我已经尝试了一些方法,但是没有一个起作用.任何帮助表示赞赏.
I have tried a few things so far, but none of them worked. Any help is appreciated.
注意:正在执行mysql_num_fields(result);在两种情况下都给出1.
Note: Doing mysql_num_fields(result); in both cases gives 1.
推荐答案
在一些好人的帮助下,我在其他地方找到了解决方案.看来,我犯了一个愚蠢的错误,并且需要彻底了解两个MYSQL C API函数之间的区别.
I have found the solution elsewhere, thanks to the help of some good people. It seems, that I had made a silly mistake as well as needed a thorough understanding of the difference between two MYSQL C API functions.
我在这里写答案,希望它能使其他人受益.
I'm writing the answer here, in hope of it benefiting others.
错误在这里:
robe = mysql_fetch_row(result);
尽管它本身是正确的.我无法检查其结果.发生的情况是,当使用数据库中不存在的用户名执行SQL查询时,结果是一个空集(而不是一个错误).
Though it is correct in itself. I fail to check its result. What happens is that when the SQL query is performed using a username that did not exist in the DB beforehand, the result is a empty set (and not a error).
mysql_store_result和mysql_fetch_row在这里有细微差别.如果集合为空,前者将不返回NULL,而后者将返回NULL.
The mysql_store_result and mysql_fetch_row have a slight difference here. While the former will not return NULL if the set is empty, the later will.
我所要做的就是在上面的代码行之后加上以下逻辑:
All I have to do is add a check after the above line with the logic:
if (robe == NULL) {
//error occured
} else { //go on
}
这篇关于在MYSQL C API中处理SELECT查询结果的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!