我又研究了一下
MySQL
提供的
C
语言的
API
,
因为上次我提供的页面有创建远程用户
以及
修改
mysql
用户名和密码的
项
,
所以
CGI
里面必须调用
mysql
的
C API
才能完成。
经过确认,
这些都可以被
MySQL C API
所支持。
比如 create database , create table , modify table , index 等等。
我们 CGI 里面所要做的就是 调用 MySQL C API 来修改 MySQL 数据库里面的 mysql 数据库里面的 user 表即可。
下面是个例子 :
1> 连接 MySQL 数据库,并 use mysql;
2> select * from user; // 查询表里面的数据
3> 打印即可
编译:
gcc -o mysql_example mysql_example.c -I/usr/local/include/mysql -L /usr/local/lib/mysql/ -lmysqlclient
执行结果:
#shell> ./mysql_example
比如 create database , create table , modify table , index 等等。
我们 CGI 里面所要做的就是 调用 MySQL C API 来修改 MySQL 数据库里面的 mysql 数据库里面的 user 表即可。
下面是个例子 :
1> 连接 MySQL 数据库,并 use mysql;
2> select * from user; // 查询表里面的数据
3> 打印即可
编译:
gcc -o mysql_example mysql_example.c -I/usr/local/include/mysql -L /usr/local/lib/mysql/ -lmysqlclient
执行结果:
#shell> ./mysql_example
CODE:
localhost root *E6CC90B878B948C35E92B003C792C46C58C4AF40 Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0
boblinux root Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0
boblinux N N N N N N N N N N N N N N N N N N N N N N N N N N 0 0 0 0
localhost N N N N N N N N N N N N N N N N N N N N N N N N N N 0 0 0 0
% admin *4ACFE3202A5FF5CF467898FC58AAB1D615029441 Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0
boblinux root Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0
boblinux N N N N N N N N N N N N N N N N N N N N N N N N N N 0 0 0 0
localhost N N N N N N N N N N N N N N N N N N N N N N N N N N 0 0 0 0
% admin *4ACFE3202A5FF5CF467898FC58AAB1D615029441 Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0
代码:
CODE:
/*
载入相关头文件
*/
//#include <winsock2.h>
// 在 windows 平台,假设链接时发现很多链接错误的时候,需要加上对 winsock2.h 的引用。
#include <stdio.h>
#include <mysql/mysql.h>
main() {
MYSQL mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int numrows, numcols, c;
char query[] = "SELECT * FROM user";
mysql_init(&mysql);
/* 连接到数据库 */
if (!mysql_real_connect(&mysql, "172.21.5.179", "admin", "admin", "mysql", 0, NULL, 0))
{
// 访问失败输出错误信息
fprintf(stderr, "Failed to connect to database: Error %d:%s/n", mysql_errno(&mysql),mysql_error(&mysql));
return -1;
}
/* 执行一个查询 */
if (mysql_query(&mysql, query))
{
// 查询失败输出错误信息
fprintf(stderr, "Error executing query: Error %d: %s/n", mysql_errno(&mysql), mysql_error(&mysql));
}
/* 处理查询结果 */
// 25.2.3.47. mysql_num_rows()
// my_ulonglong mysql_num_rows(MYSQL_RES *result)
//
// 描述
//
// 返回结果集中的行数。
//
// mysql_num_rows() 的使用取决于是否采用了 mysql_store_result() 或 mysql_use_result() 来返回结果集。如果使用了 mysql_store_result() ,可以立刻调用 mysql_num_rows() 。如果使用了 mysql_use_result() , mysql_num_rows() 不返回正确的值,直至检索了结果集中的所有行为止。
//
// 返回值
//
// 结果集中的行数。
result = mysql_store_result(&mysql); //mysql_use_result() 和 mysql_store_result()
if (!result)
{
// 查询结果出错
fprintf(stderr, "Error executing query: Error %d: %s/n", mysql_errno(&mysql), mysql_error(&mysql));
}
/* 查找查询结果的列数 */
numcols = mysql_num_fields(result);
numrows = mysql_num_rows(result);
printf("filds = %d/n",numcols);
printf("rows = %d/n",numrows);
printf("/n/n");
/* 循环显示查询结果 */
while (row = mysql_fetch_row(result)) {
for(c=0; c<numcols; c++) {
printf("%s/t", row[c]);
}
printf("/n");
}
}
//#include <winsock2.h>
// 在 windows 平台,假设链接时发现很多链接错误的时候,需要加上对 winsock2.h 的引用。
#include <stdio.h>
#include <mysql/mysql.h>
main() {
MYSQL mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int numrows, numcols, c;
char query[] = "SELECT * FROM user";
mysql_init(&mysql);
/* 连接到数据库 */
if (!mysql_real_connect(&mysql, "172.21.5.179", "admin", "admin", "mysql", 0, NULL, 0))
{
// 访问失败输出错误信息
fprintf(stderr, "Failed to connect to database: Error %d:%s/n", mysql_errno(&mysql),mysql_error(&mysql));
return -1;
}
/* 执行一个查询 */
if (mysql_query(&mysql, query))
{
// 查询失败输出错误信息
fprintf(stderr, "Error executing query: Error %d: %s/n", mysql_errno(&mysql), mysql_error(&mysql));
}
/* 处理查询结果 */
// 25.2.3.47. mysql_num_rows()
// my_ulonglong mysql_num_rows(MYSQL_RES *result)
//
// 描述
//
// 返回结果集中的行数。
//
// mysql_num_rows() 的使用取决于是否采用了 mysql_store_result() 或 mysql_use_result() 来返回结果集。如果使用了 mysql_store_result() ,可以立刻调用 mysql_num_rows() 。如果使用了 mysql_use_result() , mysql_num_rows() 不返回正确的值,直至检索了结果集中的所有行为止。
//
// 返回值
//
// 结果集中的行数。
result = mysql_store_result(&mysql); //mysql_use_result() 和 mysql_store_result()
if (!result)
{
// 查询结果出错
fprintf(stderr, "Error executing query: Error %d: %s/n", mysql_errno(&mysql), mysql_error(&mysql));
}
/* 查找查询结果的列数 */
numcols = mysql_num_fields(result);
numrows = mysql_num_rows(result);
printf("filds = %d/n",numcols);
printf("rows = %d/n",numrows);
printf("/n/n");
/* 循环显示查询结果 */
while (row = mysql_fetch_row(result)) {
for(c=0; c<numcols; c++) {
printf("%s/t", row[c]);
}
printf("/n");
}
}