M
mydancake
I need to create an apache module which simply connects to a mysql
database, does some processing and returns some data to the users
browser.
I bet your wondering why I want to use an apache module rather than
PHP or Perl. I did some tests and PHP can handle around 30-40 requests
per second where as an apache module
it can handle 200+.
The code below is just a simple and crude test. I've had some some
trouble with the mysql part, for example, if I try to use
mysql_real_query I get undefined symbol:
mysql_real_query.
Also what would be the best way to handle connections to a mysql
database and where should I connect and close connections. Obviously I
don't want to connect on each request
received. Any example code would be appreciated.
Please be gentle, I'm fairly new to C.
#include <httpd.h>
#include <http_protocol.h>
#include <http_config.h>
#include <mysql.h>
static int mytest_handler(request_rec* r)
{
if (!r->handler || strcmp(r->handler, "mytest"))
return DECLINED;
if (r->method_number != M_GET)
return HTTP_METHOD_NOT_ALLOWED;
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "";
char *database = "mydatabase";
if (!mysql_real_connect(conn, server, user, password, database, 0,
NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
}
ap_set_content_type(r, "text/html;charset=ascii");
/* send SQL query */
if (mysql_real_query(conn, "SELECT * FROM mytable LIMIT 0, 10", 39))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(0);
}
res = mysql_use_result(conn);
while ((row = mysql_fetch_row(res)) != NULL) {
ap_rputs("Got a row", r);
}
/* Release memory used to store results and close connection */
mysql_free_result(res);
mysql_close(conn);
return OK;
}
static void register_hooks(apr_pool_t* pool)
{
ap_hook_handler(mytest_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
module AP_MODULE_DECLARE_DATA mytest_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
NULL,
register_hooks
};
database, does some processing and returns some data to the users
browser.
I bet your wondering why I want to use an apache module rather than
PHP or Perl. I did some tests and PHP can handle around 30-40 requests
per second where as an apache module
it can handle 200+.
The code below is just a simple and crude test. I've had some some
trouble with the mysql part, for example, if I try to use
mysql_real_query I get undefined symbol:
mysql_real_query.
Also what would be the best way to handle connections to a mysql
database and where should I connect and close connections. Obviously I
don't want to connect on each request
received. Any example code would be appreciated.
Please be gentle, I'm fairly new to C.
#include <httpd.h>
#include <http_protocol.h>
#include <http_config.h>
#include <mysql.h>
static int mytest_handler(request_rec* r)
{
if (!r->handler || strcmp(r->handler, "mytest"))
return DECLINED;
if (r->method_number != M_GET)
return HTTP_METHOD_NOT_ALLOWED;
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "";
char *database = "mydatabase";
if (!mysql_real_connect(conn, server, user, password, database, 0,
NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
}
ap_set_content_type(r, "text/html;charset=ascii");
/* send SQL query */
if (mysql_real_query(conn, "SELECT * FROM mytable LIMIT 0, 10", 39))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(0);
}
res = mysql_use_result(conn);
while ((row = mysql_fetch_row(res)) != NULL) {
ap_rputs("Got a row", r);
}
/* Release memory used to store results and close connection */
mysql_free_result(res);
mysql_close(conn);
return OK;
}
static void register_hooks(apr_pool_t* pool)
{
ap_hook_handler(mytest_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
module AP_MODULE_DECLARE_DATA mytest_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
NULL,
register_hooks
};