php外包 老杨

php外包,ecshop,zencart,谷歌地图二次开发,linux网站维护 QQ:290359552

2009-09-20 15:30

php扩展连接到mysql

config.m4

PHP_ARG_ENABLE(yqr_func, whether to enable yqr functions support,
[ --enable-yqr_func   Enable yqr_func support])

if test "$PHP_YQR_FUNC" = "yes"; then
AC_DEFINE(HAVE_YQR_FUNC, 1, [Whether you have yqr func])
PHP_NEW_EXTENSION(yqr_func, yqr_func.c, $ext_shared)
fi

php_yqr_func.h

#ifndef PHP_YQR_FUNC_H
#define PHP_YQR_FUNC_H 1

#define PHP_YQR_FUNC_VERSION "1.0"
#define PHP_YQR_FUNC_EXTNAME "yqr_func"

PHP_FUNCTION(yqr_name);
PHP_FUNCTION(yqr_mysql);
extern zend_module_entry yqr_func_module_entry;
#define phpext_yqr_func_ptr &yqr_func_module_entry

#endif

yqr_func.c

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
//以上几行是给mysql用的

#include "php.h"
#include "php_yqr_func.h"

static function_entry yqr_func_functions[] = {
PHP_FE(yqr_name , NULL )
PHP_FE(yqr_mysql , NULL )
{NULL, NULL, NULL}
};

zend_module_entry yqr_func_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_YQR_FUNC_EXTNAME,
yqr_func_functions,
NULL,
NULL,
NULL,
NULL,
NULL,
#if ZEND_MODULE_API_NO >= 20010901
PHP_YQR_FUNC_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_YQR_FUNC
ZEND_GET_MODULE(yqr_func)
#endif

PHP_FUNCTION(yqr_name)
{
RETURN_STRING("My name is Yang Qing Rong\n", 1);
}





PHP_FUNCTION(yqr_mysql )
{
MYSQL *mysql;
MYSQL_RES *result;
MYSQL_ROW row;
char sql[1024];
int query_result;

mysql=mysql_init(NULL);
if ( ! mysql_real_connect(mysql,"localhost","admin", "123456","db_test",0,NULL,0 )  )
{
RETURN_STRING( "connect time out" , 1 );
}

sprintf( sql ,  "select * from article limit 0,20" ) ;
query_result =mysql_query(mysql , sql ) ;
if ( query_result )
{
printf("查询出错了: %s\n",   mysql_error(mysql));
}

result=mysql_use_result(mysql);

int i;

while( row = mysql_fetch_row( result ) )
{
for( i=0; i<  mysql_num_fields( result )  ; i++ )
{
php_printf( "%s\n" , row[i]? row[i] :"null" );
}
}
mysql_close(mysql);
RETURN_TRUE;
}


附:

那哥们说用php扩展比用mysql.so快,于是我装了mysql-dev,弄了个扩展出来,运行看了一下,没有感到快了。估计是数据少的缘故吧。

can not find mysql.h on you machine?
shell>sudo apt-get install libmysqlclient15-dev
you will find /usr/include/mysql/mysql.h
shell>./configure --enable-yqr_func
在/ext/yqr_func/Makefile 中添加以下这一句(注意-L后不能有空格,否则报错。):
-I /usr/include/mysql -L/usr/lib/mysql -lmysqlclient
改变后的INCLUDES看起来是这样子的:

INCLUDES = -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib  -I /usr/include/mysql -L/usr/lib/mysql -lmysqlclient -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64


yqr@yqr-desktop:~/source/php-5.3.0/ext/yqr_func$ php -r 'echo yqr_mysql();'

30
2008-05-28 08:00:00
22222
/upload/article_image/small_496842ff62651.jpg
/upload/article_image/496842ff62651.jpg

0
3
30
2007-06-15 01:09:57
222222
/upload/article_image/small_49683f20b3657.jpg
/upload/article_image/49683f20b3657.jpg

蓝色的字是在数据库取出来的数据。
评论