About trying to develop MYSQL extensions for PHP

  • 2020-04-01 21:35:23
  • OfStack

I. preliminary preparation
To develop MYSQL extensions, of course, you must first install MYSQL
Download link: http://l9.yunpan.cn/lk/sVRdzxirPNSbw)

You must select the red option because developing MYSQL extensions requires header files and MYSQL's dynamic library. Otherwise the extension will not compile successfully.

The main purpose of this extension is to familiarize yourself with the ZEND API and to understand how to provide the PHP API.
The implementation of PHP API is:
PHP_FUNCTION (my_mysql_connect);
PHP_FUNCTION (my_mysql_close);
PHP_FUNCTION (my_mysql_get_conn); // access to mysql resources through the link pool, the connection pool is simply automatically expanded, not shrunk.
PHP_FUNCTION (my_mysql_select_db); // switch database
PHP_FUNCTION (my_mysql_ping);
PHP_FUNCTION (my_mysql_query); // execute SQL, which can be a SELECT or INSERT, UPDATE, DELETE, etc., with or without a return value
PHP_FUNCTION (my_mysql_fetch_assoc); // gets the returned result set
PHP_FUNCTION (my_mysql_get_insert_id); // gets the auto-id of the last INSERT

Two, development ideas
1. Create the my_mysql extension with ext_skel_win32.php(skeleton tool).
2. Use VS2008 to open my_myqsl.dsp and edit project properties.
        2.1 switch to the Release version, and the default is the DEBUG version
        2.2 right click project properties - "general properties -" C/C++- "preprocessor -" preprocessor definition to remove the ZTS=1, because we will compile to NTS version. (non-thread safe)
        2.3 right-click project properties - "general properties -" C/C++- "general -" additional include directory, new MYSQL_ROOT\include. Introduces the header file search path.
                  This way, when you use #include "mysql.h", you won't have the problem of searching endless files
        2.4 right-click project properties - "general properties -" linker - "general -" additional library directory, new MYSQL_ROOT\lib\opt, compile the extension need to link DLL in it. (also don't forget to introduce the path to php5nts.lib, which is required at compile time for any extension.)
        2.5 right-click project properties - "general properties -" linker - "input -" additional dependencies added libmysql.lib, intended to set the need to link at compile time DLL.
3. Create a new PHP resource type for my_mysql.
4. Implement PHP API for PHP interface.
5. Compile the extension, copy the generated php_my_mysql.dll into PHP_ROOT\ext, and modify php.ini to add extension=ext\php_my_mysql.dll.
6. Write a PHP file, call the functions provided in the extension, and debug.

@mysql_root: represents the installation path for mysql
@php_root: represents the path of PHP

Start coding
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:                                                              |
  +----------------------------------------------------------------------+
*/

#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);

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)


#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 

Interface test 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;

Test results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201304/201304170001.jpg ">


Related articles: