From 084318f404149d272624c2a13cb8bc726eb7dd3d Mon Sep 17 00:00:00 2001 From: wang_chengh <13383929+wang_chengh@user.noreply.gitee.com> Date: Mon, 18 Sep 2023 19:20:43 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E5=A4=A9cgi=E5=89=8D?= =?UTF-8?q?=E5=90=8E=E7=AB=AF=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cgi/C_mysql_r_json.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 cgi/C_mysql_r_json.c diff --git a/cgi/C_mysql_r_json.c b/cgi/C_mysql_r_json.c new file mode 100644 index 0000000..e7ae264 --- /dev/null +++ b/cgi/C_mysql_r_json.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +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; +}