我的一个脚本出现内部服务器错误。我正在使用 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);
HTML 表单通过 POST 提交到此脚本(上面可以看到其中的一部分)。当我提交预先存在于数据库中的用户名时,我没有收到任何错误。一切正常。
当我提交上述表(testtab)中不存在的用户名时,问题就出现了。嗯,我收到 500 内部服务器错误。我也查看了 Apache 错误日志:“ header 之前的脚本输出结束”。
到目前为止,我已经尝试了一些方法,但没有一个起作用。任何帮助表示赞赏。
注意:执行 mysql_num_fields(result);在这两种情况下都给出 1。
最佳答案
首先,您绝对不应该将密码存储在数据库中,尤其是可通过在线服务访问的密码。 exit(0)
表示成功。它还会在完成之前将您的输出短路。您不能只在生成输出的过程中调用 exit(0) 。使用某种“数据不可用”字符串代替。
关于mysql - 在 MYSQL C API 中处理 SELECT 查询结果时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52797828/