第一天cgi前后端链接

This commit is contained in:
wang_chengh 2023-09-18 19:20:43 +08:00
parent 7a15326192
commit 084318f404
1 changed files with 47 additions and 0 deletions

47
cgi/C_mysql_r_json.c Normal file
View File

@ -0,0 +1,47 @@
#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
conn = mysql_init(NULL);
if (conn == NULL) {
fprintf(stderr, "mysql_init() failed\n");
exit(1);
}
if (mysql_real_connect(conn, "localhost", "root", "root", "router", 3306,
NULL, 0) == NULL) {
fprintf(stderr, "mysql_real_connect() failed\n");
mysql_close(conn);
exit(1);
}
if (mysql_query(conn,
"SELECT JSON_OBJECT('username', username, 'password', "
"password) FROM users")) {
fprintf(stderr, "SELECT query failed. Error: %s\n", mysql_error(conn));
mysql_close(conn);
exit(1);
}
res = mysql_store_result(conn);
if (res == NULL) {
fprintf(stderr, "mysql_store_result() failed\n");
mysql_close(conn);
exit(1);
}
while ((row = mysql_fetch_row(res))) {
printf("%s\n", row[0]);
}
mysql_free_result(res);
mysql_close(conn);
return 0;
}