【发布时间】:2019-03-18 18:22:11
【问题描述】:
我的一个脚本出现内部服务器错误。我正在使用 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 错误日志:“头文件前的脚本输出结束”。
到目前为止,我已经尝试了一些方法,但都没有奏效。任何帮助表示赞赏。
注意:做 mysql_num_fields(result);在这两种情况下都给出 1。
【问题讨论】:
-
您没有显示整个程序或整个输出集,但我敢打赌,除了您在此处显示的内容之外,还有一些东西会在查询成功时执行,而在查询时不会执行失败。因此,我担心在每次失败时使用
exit(0)。 -
基本上,您需要一个类似于 finalize 步骤的东西,它总是会执行,即使出现故障也是如此。诚然只是一个猜测,我从不将 Web 服务器连接到数据库。
标签: mysql c mariadb mysql-connector-c