一、前期准備
開發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 |
| [email protected] 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 */
接口測試test.php
代碼如下:
<?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;
測試結果: