关于尝试开发PHP的MYSQL扩展的使用
一、前期准备
开发MYSQL扩展,当然得先安装MYSQL
下载mysql-essential-5.1.65-win32
必须选上红色的选项,因为开发MYSQL扩展时,需要头文件和mysql的动态库。否则扩展无法编译成功。
这个扩展的目的主要是熟悉ZEND API,并且理解如何提供PHP API。
实现的PHP API有:
PHP_FUNCTION(my_mysql_connect);
PHP_FUNCTION(my_mysql_close);
PHP_FUNCTION(my_mysql_get_conn);//通过链接池获取mysql资源,这个连接池只是简单的自动扩容,并没有收缩。
PHP_FUNCTION(my_mysql_select_db);//切换数据库
PHP_FUNCTION(my_mysql_ping);
PHP_FUNCTION(my_mysql_query);//执行sql,可以是SELECT或INSERT、UPDATE、DELETE等有返回值与无返回值的查询语句
PHP_FUNCTION(my_mysql_fetch_assoc);//获取返回结果集
PHP_FUNCTION(my_mysql_get_insert_id);//获取上一次执行INSERT的自增ID
二、开发思路
1.通过ext_skel_win32.php(骨架工具)创建my_mysql扩展。
2.使用VS2008打开my_myqsl.dsp,编辑项目属性。
2.1 切换为Release版本,默认是DEBUG版本
2.2 右键项目属性-》通配属性-》C/C++-》预处理器-》预处理器定义除去其中的ZTS=1,因为我们将编译为NTS版本。(非线程安全)
2.3 右键项目属性-》通配属性-》C/C++-》常规-》附加包含目录,新增MYSQL_ROOT\include。引入头文件搜索路径。
这样当使用#include "mysql.h"时,就不会出现搜索不到头文件的错误
2.4 右键项目属性-》通配属性-》链接器-》常规-》附加库目录,新增MYSQL_ROOT\lib\opt,编译扩展时需要链接的DLL都在其中。(另外不要忘记引入php5nts.lib的路径,这是任一扩展编译时都需要引入的)
2.5 右键项目属性-》通配属性-》链接器-》输入-》附加依赖项新增libmySQL.lib,意在编译时设定需要链接的DLL。
3.新建my_mysql新的PHP资源类型。
4.实现PHP API,供给PHP接口使用。
5.编译扩展,拷贝生成的php_my_mysql.dll到PHP_ROOT\ext下,并修改php.ini,增加extension=ext\php_my_mysql.dll。
6.编写PHP文件,调用扩展中提供的函数并调试。
@MYSQL_ROOT:表示mysql的安装路径
@PHP_ROOT:表示php所在路径
三、开始编码
ext\my_mysql\php_my_mysql.h
代码如下:
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2012 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifndef PHP_MY_MYSQL_H
#define PHP_MY_MYSQL_H
extern zend_module_entry my_mysql_module_entry;
#define phpext_my_mysql_ptr &my_mysql_module_entry
#ifdef PHP_WIN32
# define PHP_MY_MYSQL_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
# define PHP_MY_MYSQL_API __attribute__ ((visibility("default")))
#else
# define PHP_MY_MYSQL_API
#endif
#ifdef ZTS
#include "TSRM.h"
#endif
PHP_MINIT_FUNCTION(my_mysql);
PHP_MSHUTDOWN_FUNCTION(my_mysql);
PHP_RINIT_FUNCTION(my_mysql);
PHP_RSHUTDOWN_FUNCTION(my_mysql);
PHP_MINFO_FUNCTION(my_mysql);
PHP_FUNCTION(my_mysql_connect);
PHP_FUNCTION(my_mysql_close);
PHP_FUNCTION(my_mysql_get_conn);
PHP_FUNCTION(my_mysql_select_db);
PHP_FUNCTION(my_mysql_ping);
PHP_FUNCTION(my_mysql_query);
PHP_FUNCTION(my_mysql_fetch_assoc);
PHP_FUNCTION(my_mysql_get_insert_id);
/*
Declare any global variables you may need between the BEGIN
and END macros here:
*/
ZEND_BEGIN_MODULE_GLOBALS(my_mysql)
long max_connection;
long default_connection;
char *driver_name;
char *host;
char *pwd;
char *user;
long port;
ZEND_END_MODULE_GLOBALS(my_mysql)
/* In every utility function you add that needs to use variables
in php_my_mysql_globals, call TSRMLS_FETCH(); after declaring other
variables used by that function, or better yet, pass in TSRMLS_CC
after the last function argument and declare your utility function
with TSRMLS_DC after the last declared argument. Always refer to
the globals in your function as MY_MYSQL_G(variable). You are
encouraged to rename these macros something shorter, see
examples in any other php module directory.
*/
#ifdef ZTS
#define MY_MYSQL_G(v) TSRMG(my_mysql_globals_id, zend_my_mysql_globals *, v)
#else
#define MY_MYSQL_G(v) (my_mysql_globals.v)
#endif
#endif /* PHP_MY_MYSQL_H */
<?php
// $mysql = my_mysql_connect("localhost", "root", "", "test", 3306);
// var_dump($mysql);
// // my_mysql_close($mysql);
// sleep(10);
$conn = array();
$conn[] = my_mysql_get_conn();
$conn[] = my_mysql_get_conn();
// $conn[] = my_mysql_get_conn();
// $conn[] = my_mysql_get_conn();
// $conn[] = my_mysql_get_conn();
// $conn[] = my_mysql_get_conn();
// $conn[] = my_mysql_get_conn();
// $conn[] = my_mysql_get_conn();
// $conn[] = my_mysql_get_conn();
// $conn[] = my_mysql_get_conn();
// $conn[] = my_mysql_get_conn();
// $conn[] = my_mysql_get_conn();
// print_r($conn);
var_dump($conn[0]);
// my_mysql_select_db($conn[0], "test");
// mysql_query("show processlist", $conn[0]);
// print_r($status);
// sleep(1);
my_mysql_select_db($conn[0], "mysql");
// sleep(1);
my_mysql_select_db($conn[1], "test");
my_mysql_ping($conn[1]);
$result = my_mysql_query($conn[1], "select * from test");
var_dump($result);
$arr = my_mysql_fetch_assoc($result);
my_mysql_query($conn[1], "INSERT INTO test VALUES(id, 'abc')");
$insert_id = my_mysql_get_insert_id($conn[1]);
print_r($arr);
echo $insert_id;
测试结果: