在 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
这是我脚本的相应部分:
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);
A HTML 表单通过 POST 提交到此脚本(上面显示了其中的一部分)。当我提交预先存在于数据库中的用户名时,我没有收到任何错误。一切正常。
问题出现了,当我提交的用户名在上述 table(testtab) 中不存在时。嗯,我收到 500 Internal Server Error。我也查看了 Apache 错误日志:"End of Script output before Headers"。
到目前为止,我已经尝试了一些方法,但其中 none 行得通。任何帮助表示赞赏。
注:做mysql_num_fields(结果);在这两种情况下都给出 1。
首先,您永远不应将密码存储在数据库中,尤其是可通过在线服务访问的数据库。 exit(0)
表示成功。它还会在完成之前将您的输出短路。您不能只在生成输出的过程中调用 exit(0)。请改用某种 "data not available" 字符串。
我在别处找到了解决办法,感谢一些好心人的帮助。看来,我犯了一个愚蠢的错误,并且需要彻底了解两个 MYSQL C API 函数之间的区别。
我把答案写在这里,希望对大家有所帮助。
错误在这里:
robe = mysql_fetch_row(result);
虽然它本身是正确的。我没有检查它的结果。发生的情况是,当使用预先在数据库中不存在的用户名执行 SQL 查询时,结果是一个空集(而不是错误)。
mysql_store_result和mysql_fetch_row这里略有不同。如果集合为空,前者不会 return NULL,而后者会。
我所要做的就是在上面的逻辑行之后添加一个检查:
if (robe == NULL) {
//error occured
} else { //go on
}
我的一个脚本出现内部服务器错误。我正在使用 MYSQL C API。 https://dev.mysql.com/doc/refman/5.6/en/c-api.html
这是我脚本的相应部分:
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);
A HTML 表单通过 POST 提交到此脚本(上面显示了其中的一部分)。当我提交预先存在于数据库中的用户名时,我没有收到任何错误。一切正常。
问题出现了,当我提交的用户名在上述 table(testtab) 中不存在时。嗯,我收到 500 Internal Server Error。我也查看了 Apache 错误日志:"End of Script output before Headers"。
到目前为止,我已经尝试了一些方法,但其中 none 行得通。任何帮助表示赞赏。
注:做mysql_num_fields(结果);在这两种情况下都给出 1。
首先,您永远不应将密码存储在数据库中,尤其是可通过在线服务访问的数据库。 exit(0)
表示成功。它还会在完成之前将您的输出短路。您不能只在生成输出的过程中调用 exit(0)。请改用某种 "data not available" 字符串。
我在别处找到了解决办法,感谢一些好心人的帮助。看来,我犯了一个愚蠢的错误,并且需要彻底了解两个 MYSQL C API 函数之间的区别。
我把答案写在这里,希望对大家有所帮助。
错误在这里:
robe = mysql_fetch_row(result);
虽然它本身是正确的。我没有检查它的结果。发生的情况是,当使用预先在数据库中不存在的用户名执行 SQL 查询时,结果是一个空集(而不是错误)。
mysql_store_result和mysql_fetch_row这里略有不同。如果集合为空,前者不会 return NULL,而后者会。
我所要做的就是在上面的逻辑行之后添加一个检查:
if (robe == NULL) {
//error occured
} else { //go on
}