About trying to develop PHP's MySQL extensions using the _c language

Source: Internet
Author: User
Tags zts

First, preliminary preparation
To develop MySQL extensions, of course, you have to install MySQL first
Download Mysql-essential-5.1.65-win32

The red option must be selected because the MySQL extension requires a header file and a MySQL dynamic library. Otherwise the extension cannot compile successfully.

The purpose of this extension is primarily to familiarize yourself with the Zend API and to understand how to provide the PHP API.
The PHP APIs implemented are:
Php_function (My_mysql_connect);
Php_function (My_mysql_close);
Php_function (My_mysql_get_conn)//through the link pool to obtain MySQL resources, this connection pool is simply automatic expansion, and does not shrink.
Php_function (my_mysql_select_db);//Switch database
Php_function (my_mysql_ping);
Php_function (my_mysql_query)//Execute SQL, can be a select or INSERT, UPDATE, delete, and so on have return value and no return value of the query statement
Php_function (MY_MYSQL_FETCH_ASSOC)//Get return result set
Php_function (my_mysql_get_insert_id);//Get the self-ID of the last insert executed

Second, the development of ideas
1. Create My_mysql extensions by ext_skel_win32.php (Skeleton tool).
2. Use VS2008 to open MY_MYQSL.DSP, edit item properties.
2.1 Switch to release version, default is Debug version
2.2 Right-Key item Properties-"Wildcard properties-" c/c++-"preprocessor-" preprocessor definition removes the Zts=1, as we will compile to the NTS version. (Not thread safe)
2.3 Right Key Item Properties-"Wildcard properties-" c/c++-"general-" additional include directory, add Mysql_root\include. Introduces the header file search path.
This way, when you use #include "mysql.h", there is no error in searching for the header file.
2.4 Right-Key Item Properties-"Wildcard properties-" linker-general-"Additional library directories, new mysql_root\lib\opt, DLLs that need to be linked when compiling extensions." (also, don't forget to introduce the php5nts.lib path, which is required for any extended compilation)
2.5 Right-Key Item Properties-"Wildcard properties-" linker-"input-" additional dependencies are added to the libmySQL.lib to set up DLLs that need to be linked at compile time.
3. Create a new My_mysql PHP resource type.
4. Implement PHP API, provide PHP interface to use.
5. Compile extensions, copy generated php_my_mysql.dll to Php_root\ext, and modify php.ini to increase extension=ext\php_my_mysql.dll.
6. Write PHP files, invoke the functions provided in the extension and debug.

@MYSQL_ROOT: Represents the installation path for MYSQL
@PHP_ROOT: Represents the path where PHP resides

Three, start coding
Ext\my_mysql\php_my_mysql.h

Copy Code code as follows:

/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2012 the PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| That's 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 don't receive a copy of the PHP license and are unable to | did
| 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 your 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 for you add this needs to use variables
In Php_my_mysql_globals, call Tsrmls_fetch (); After declaring
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 on the last declared argument. Always refer to
The globals in your function as MY_MYSQL_G (variable). You are
Encouraged to rename macros something shorter
Examples in or 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 * *


Interface Test test.php
Copy Code code as follows:

<?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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.