---------------------------------------------------
| Date: 2004-07-27 02:54:00 |
| Filename: webinc.c |
| Author: mind@metalshell.com |
| |
| http://www.metalshell.com/ |
---------------------------------------------------
/* Website in C - mind@metalshell.com
*
* This code was meant to show the flexibility
* and speed of coding a site in C. [ Nobody Likes PHP! =) ]
*
* This example will show how to create a fully developed
* website in C, using mysql (optional).
*
* I did a speed test; Printing 2,000 lines containing 100 'A's
* per line using php and C; the results were: C (0.120s) PHP (0.605s)
*
* Note: I used a version of my own printing example to make
* the HTML output look alot nicer.. eprint() is optional.
*
* libmysqlclient.a: path may be different.
* To compile: gcc example.c -o example.cgi -lz /usr/lib/mysql/libmysqlclient.a
*
* to view VIA web browser: turn ExecCGI on in httpd.conf
* and move example.cgi to your html folder.
*/
#include
#include
#include
#include
/* header path may be different */
#include "/usr/include/mysql/mysql.h"
/* To setup your mysql db so the example will work please
* open mysql; and paste the following:
*
* create database Cexample;
*
* use Cexample;
* create TABLE person (
* id mediumint(9) NOT NULL auto_increment,
* name char(40) NOT NULL,
* birth char(40) NOT NULL,
* addy char(40) NOT NULL,
* phone char(40) NOT NULL,
* PRIMARY KEY (id)
* );
*
* insert into person VALUES( NULL, 'John Doe', '01-20-66',
* 'N. Burboun ST.', '800-123-4567' );
*
* insert into person VALUES( NULL, 'Jane Doe', '12-09-72',
* 'S. Burboun ST.', '800-987-6543');
*
*/
/* edit USER, and PASS */
#define MySQL_HOST "localhost"
#define MySQL_USER "ChangeME"
#define MySQL_PASS "ChangeME"
#define MySQL_DATA "Cexample"
MYSQL_RES *res;
MYSQL_ROW row;
int TabMe = 0;
void eprint(int b, char *fmt, ...);
void ShowError(char *x, char *e);
void StartHtml();
void GetBirth(int x);
void GetAddress(int x);
void GetPhone(int x);
void GetShowAll(int x);
int StartQuery();
/* This structure is for our apache string parser
* when it parses for example id=1&0 our parser will
* execute GetBirth(0);
*/
struct
{
int id;
void (*func)(int);
}
DefExt[]=
{
{ 1, GetBirth },
{ 2, GetAddress },
{ 3, GetPhone },
{ 4, GetShowAll },
{ 0, NULL },
};
int main()
{
MYSQL mysql;
char query[100];
/* Initial start of our site's source */
printf("Content-Type: text/html\n\n");
/* Check to see if any arguments were passed through
* our program. If not show front page. */
if ((StartQuery()) == -1)
{
StartHtml();
if (!(mysql_connect(&mysql, MySQL_HOST, MySQL_USER, MySQL_PASS)))
ShowError(NULL, "MySQL SERVER down");
if (mysql_select_db(&mysql, MySQL_DATA))
ShowError(NULL, "MySQL database error");
snprintf(query, sizeof(query), "select * from person order by name");
if (mysql_query(&mysql,query))
ShowError(NULL, "MySQL server error");
res = mysql_store_result(&mysql);
while ((row = mysql_fetch_row(res)))
{
eprint( 0, "%s - \n", row[1]);
eprint( 0, "Birth Day\n", row[0]);
eprint( 0, " - \n");
eprint( 0, "Address\n", row[0]);
eprint( 0, " - \n");
eprint( 0, "Phone\n", row[0]);
eprint( 0, " - \n");
eprint( 0, "Show All
\n", row[0]);
}
mysql_close(&mysql);
mysql_free_result(res);
eprint(-1, "