在 MYSQL C API 中处理 SELECT 查询结果的问题
我的一个脚本出现内部服务器错误.我正在使用 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
");
printf("Could not connect
");
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
");
printf("Something went wrong with Sprintf
");
exit(0); }
int bos = mysql_real_query(con, somequery, strlen(somequery));
if (bos != 0) {
printf("Content-type: text/html
");
printf("The query produced no result
");
exit(0); }
result = mysql_store_result(con);
if (result == NULL) {
printf("Content-type: text/html
");
printf("No Result Set Produced
");
exit(0); }
robe = mysql_fetch_row(result);
char *passdb = robe[0];
printf("Content-type: text/html
");
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,而后者会.
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
}
相关文章