{"id":415,"date":"2012-06-04T14:45:50","date_gmt":"2012-06-04T06:45:50","guid":{"rendered":"http:\/\/www.kiccleaf.com\/?p=415"},"modified":"2020-08-28T12:17:29","modified_gmt":"2020-08-28T04:17:29","slug":"linux%e4%b8%8bc%e8%af%bb%e5%8f%96mysql%e4%b8%ad%e6%96%87%e4%b9%b1%e7%a0%81%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"http:\/\/www.kiccleaf.com\/?p=415","title":{"rendered":"Linux\u4e0bc\u8bfb\u53d6MysqL\u4e2d\u6587\u4e71\u7801\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"\n<p>\u5728\u7f16\u5199\u63a5\u53e3API\u65f6\uff0c\u53d1\u73b0\u4e2d\u6587\u5b57utf8\u8f93\u5165\u7684\u5728linux\u4e0b\u91c7\u7528c\u8bfb\u53d6\u663e\u793a\u4e3a&#8221;??&#8221;\u95ee\u53f7\uff0c\u8fd9\u662f\u7531\u4e8e\u7f16\u7801\u9020\u6210\u7684\u3002\u5f88\u7b80\u5355\u7684\u4e24\u4e2a\u5730\u65b9\u505a\u4fee\u6539\u5c31\u641e\u5b9a\u3002<br>1.\u5148\u627e\u5230mysql\u7684my.cnf\u914d\u7f6e\u6587\u4ef6\/etc\/my.cnf\u7f16\u8f91\u6dfb\u52a0<br>[mysqld]<br>default-character-set=utf8<\/p>\n\n\n\n<p>2.\u5728\u7a0b\u5e8f\u4e2d\u6dfb\u52a0 mysql_set_character_set(&amp;db,&#8221;utf8&#8243;); \u8bed\u53e5<br>\u4f8b\u5b50\uff1akiccleaf.c \u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">#include &lt;stdio.h>\n\/\/\u8fd9\u91cc\u9700\u8981\u627e\u5230\u76f8\u5e94\u5730\u5740\u4e0d\u77e5\u9053\u53ef\u4ee5\u7528\u201c find \/ -name mysql.h \u201d\u67e5\u8be2\n#include \"\/usr\/include\/mysql\/mysql.h\"\nint main()\n{\n    MYSQL db;\/*connector*\/\n    MYSQL_RES* result;\/*result buffer*\/\n    MYSQL_ROW row;\/*one row of the result*\/\n    int i;\n    if(mysql_init(&amp;db) ==NULL)\n    {\n      fprintf(stderr,\"Fail to initialize the db.\\n\");\n      return -1;\n    }\n    if(!mysql_real_connect(&amp;db,\"localhost\",\"root\",\"password\",\"kiccleaf\",3306,NULL,0))\n    {\n      fprintf(stderr,\"Fail to connect to the server\");\n      return -1;\n    }\n    \/\/\u6dfb\u52a0\u5b57\u7b26\u96c6\u9632\u6b62\u4e71\u7801\n    mysql_set_character_set(&amp;db,\"utf8\");\n    if(mysql_query(&amp;db,\"SELECT * FROM user\") != 0)\n    {\n      fprintf(stderr,\"Fail to query the db for information.\\n\");\n      return -1;\n    }\n    if ((result = mysql_store_result(&amp;db)) == NULL)\n    {\n      fprintf(stderr,\"Fail to get the result.\\n\");\n      return -1;\n    }\n\n    while((row=mysql_fetch_row(result)) != NULL)\/*fetching each row*\/\n    {\n      puts(\"================================================\");\n      printf(\"id: %s\\n\",row[0]);\n      printf(\"name: %s\\n\",row[1]);\n      printf(\"pwd: %s\\n\",row[2]);\n      printf(\"flag: %s\\n\",row[3]);\n      puts(\"================================================\");\n    }\n\n    mysql_free_result(result);\n    mysql_close(&amp;db);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u7f16\u8bd1\u65f6\u4e5f\u4f1a\u51fa\u73b0\u95ee\u9898\uff0c\u53ef\u4ee5\u6dfb\u52a0\u53c2\u6570 \u201c-lz \/usr\/lib\/mysql\/libmysqlclient.so.15.0.0\u201d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">gcc -o kiccleaf -g kiccleaf.c -lz \/usr\/lib\/mysql\/libmysqlclient.so.15.0.0<\/code><\/pre>\n\n\n\n<p>\u539f\u6765\u4e71\u7801\u7684\u60c5\u51b5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">[root@kiccleaf c]# .\/kiccleaf\n================================================\nid: 0000000001\nname: ???\npwd: 123456\nflag: 0\n================================================\n================================================\nid: 0000000002\nname: ???\npwd: 654123\nflag: 0\n================================================\n================================================\nid: 0000000003\nname: ???\npwd: 789456\nflag: 0\n================================================\n================================================\nid: 0000000004\nname: ???\npwd: 456321\nflag: 0\n================================================<\/code><\/pre>\n\n\n\n<p>\u6dfb\u52a0\u5b57\u7b26\u96c6\u540e\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">[root@kiccleaf c]# .\/kiccleaf\n================================================\nid: 0000000001\nname: \u6d4b\u8bd51\npwd: 123456\nflag: 0\n================================================\n================================================\nid: 0000000002\nname: \u6d4b\u8bd52\npwd: 654123\nflag: 0\n================================================\n================================================\nid: 0000000003\nname: \u6d4b\u8bd53\npwd: 789456\nflag: 0\n================================================\n================================================\nid: 0000000004\nname: \u6d4b\u8bd54\npwd: 456321\nflag: 0\n================================================<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u7f16\u5199\u63a5\u53e3API\u65f6\uff0c\u53d1\u73b0\u4e2d\u6587\u5b57utf8\u8f93\u5165\u7684\u5728li&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"http:\/\/www.kiccleaf.com\/?p=415\">Continue reading<span class=\"screen-reader-text\">Linux\u4e0bc\u8bfb\u53d6MysqL\u4e2d\u6587\u4e71\u7801\u89e3\u51b3\u65b9\u6848<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3,"footnotes":""},"categories":[27,4],"tags":[28,13,58,87],"class_list":["post-415","post","type-post","status-publish","format-standard","hentry","category-c","category-linux-","tag-c","tag-linux","tag-mysql","tag-87","entry"],"views":4470,"_links":{"self":[{"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=\/wp\/v2\/posts\/415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=415"}],"version-history":[{"count":1,"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=\/wp\/v2\/posts\/415\/revisions"}],"predecessor-version":[{"id":733,"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=\/wp\/v2\/posts\/415\/revisions\/733"}],"wp:attachment":[{"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=415"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}