Thread: C/C++ FastCGI
View Single Post
  #21  
Old 06-14-2005, 01:21 AM
fryk fryk is offline
Member
 
Join Date: Jun 2005
Posts: 16
Sure.

simple-test-script.php
Code:
<html><head><title>CGI C Example #3</title></head>
<body><h1>CGI C Example #3</h1>
<table border=2>

<?

$db = mysql_pconnect&#40;"localhost","root",""&#41; or die&#40;'konnekt'&#41;;
mysql_select_db&#40;"ht3"&#41; or die&#40;'db'&#41;;

$r = mysql_query&#40;"SELECT * FROM pages"&#41;;
if &#40;$r&#41;	
&#123;
	$nrows = mysql_num_rows&#40;$r&#41;;
	$num_fields = mysql_num_fields&#40;$r&#41;;
	
	printf&#40;"rows&#58; %i, cols&#58; %i

\n",$nrows,$num_fields&#41;;
	printf&#40;"<table border=2>\n"&#41;;

	while &#40; $row = mysql_fetch_array&#40;$r&#41; &#41;
	&#123;
		printf&#40;"<tr>"&#41;;
		for&#40;$j = 0; $j < $num_fields; $j++&#41;
		&#123;
			printf&#40;"<td>%s</td>", $row&#91;$j&#93; ? $row&#91;$j&#93; &#58; NULL"&#41;;
		&#125;
		printf&#40;"</tr>"&#41;;
		printf&#40;"\n"&#41;;
	&#125;

	printf&#40;"</table></body></html>\n"&#41;;
   mysql_free_result&#40;$r&#41;;

&#125;

mysql_close&#40;$db&#41;;

?>

</table></body></html>
simple-test-script.c
Code:
#include	<stdio.h>
#include	<mysql.h>
#include "fcgi_config.h"
#include <stdlib.h>
#include "fcgi_stdio.h"

int main &#40;&#41;
&#123;
	int	err = 0;
	MYSQL	dbase;

	if &#40;mysql_init&#40;&dbase&#41; == NULL&#41; err = 1;
	else
	&#123;		
		if&#40;mysql_real_connect&#40;&dbase,"localhost","root","","ht3",0,NULL,0&#41; == NULL&#41; err = 1;
	&#125;
	if&#40;err&#41;
	&#123;
		printf&#40;"

Error connecting to database</body></html>\n"&#41;;
		exit&#40;0&#41;;
	&#125;


	//
	// B E G I N   R E Q U E S T
	// 


    while &#40;FCGI_Accept&#40;&#41; >= 0&#41;
    &#123;
		char		sqlbuff&#91;255&#93;;
		MYSQL_RES	*result;
		
		printf&#40;"Content-type&#58; text/html\r\n\r\n"&#41;;
		printf&#40;"<html><head><title>CGI C Example #3</title></head>\n"&#41;;
		printf&#40;"<body><h1>CGI C Example #3&#58; %i</h1>\n",getpid&#40;&#41;&#41;;
	
		sprintf&#40;sqlbuff,"SELECT * FROM pages"&#41;;
		if&#40;mysql_real_query&#40;&dbase,sqlbuff,strlen&#40;sqlbuff&#41;&#41;&#41;
		&#123;
			printf&#40;"

SQL error</body></html>\n"&#41;;
			exit&#40;1&#41;;
		&#125;
		
		result = mysql_store_result&#40;&dbase&#41;;
		if&#40;result&#41;
		&#123;
			int			i, j, num_fields, nrows;
			MYSQL_ROW	row;;
			
			nrows = mysql_num_rows&#40;result&#41;;
			num_fields = mysql_num_fields&#40;result&#41;;
			
			printf&#40;"rows&#58; %i, cols&#58; %i

\n",nrows,num_fields&#41;;
			printf&#40;"<table border=2>\n"&#41;;

			while &#40; row = mysql_fetch_row&#40;result&#41; &#41;
			&#123;
				printf&#40;"<tr>"&#41;;
				for&#40;j = 0; j < num_fields; j++&#41;
				&#123;
					printf&#40;"<td>%s</td>", row&#91;j&#93; ? row&#91;j&#93; &#58; "NULL"&#41;;
				&#125;
				printf&#40;"</tr>"&#41;;
				printf&#40;"\n"&#41;;
			&#125;

			printf&#40;"</table></body></html>\n"&#41;;

			mysql_free_result&#40;result&#41;;
			free&#40;result&#41;;
		&#125;
		else
		&#123;
			printf&#40;"No entries</body></html>\n"&#41;;
		&#125;

    &#125; /* while */
    
    mysql_close&#40;&dbase&#41;;

    return 0;
&#125;
"SELECT * FROM pages" returns 4 rows / 5 short columns each.

I got ubuntu:
Code:
root@fryk&#58;/var/www # uname -a
Linux fryk 2.6.10-5-386 #1 Tue Apr 5 12&#58;12&#58;40 UTC 2005 i686 GNU/Linux
on p4 2.xghz 512ram

Apache2 run just from apt-get install + mod_php + mod_fastcgi (-processes 10 - same as lsws).
Lsws standard with c fastcgi as described in previous posts (php fastcgi is built in - as I get it).

I typically measured it with
Code:
ab -n 20000 -c 50 PATH
Reply With Quote