linux下apache表单与mysql的连接

之前写了linux下apache表单与c语言写的cgi程序和c语言与mysql的连接,今天这里就写下apache表单与mysql的连接:
1、创建一新空表
 CREATE TABLE num(  
    ID int(11) NOT NULL auto_increment,  
    first int(11),  
    second int(11),  
    PRIMARY KEY (ID)  
  );


程序如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mysql.h"
#define SELECT_QUERY "insert into webnum(first,second) values(%d,%d)"
int main(void)
{
	MYSQL con,*sock;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char qbuf[160];
	char *data,*p;	
	int i=1;
	char tem[4][50];

	/*与表单联系*/
	printf("content-type:text/html;charset=gb2312\n\n");
	data=getenv("QUERY_STRING");
	printf("data=%s<br>\n",data);

	p = strtok(data,"=&");
	strcpy(tem[0],p);
	while((p = strtok(NULL,"=&"))){
	strcpy(tem[i],p);
	i++;
	}
	printf("the frist word is %s<br>",tem[1]);
	printf("the second word is %s<br>",tem[3]);	

	/*与mysql连接*/
	mysql_init(&con);
	if(sock=(mysql_real_connect(&con,"localhost","root","wangsong110","foo",0,NULL,0))){
	printf("Connection success\n");
	}

	sprintf(qbuf,SELECT_QUERY,atoi(tem[1]),atoi(tem[3]));
	mysql_query(sock,qbuf);

	mysql_close(sock);

	return 0;
}

执行程序,其他步骤如上篇文章,结果如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值