關於嘗試開發PHP的MYSQL擴充的使用

來源:互聯網
上載者:User

一、前期準備
開發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 */

介面測試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;

測試結果:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.