Use of MYSQL extension for PHP Development

Source: Internet
Author: User
Tags zts

I. Preparations
To develop MYSQL extensions, you must install MYSQL first.
Download mysql-essential-5.1.65-win32

The red option must be selected because the header file and MYSQL dynamic library are required during mysql extension development. Otherwise, the extension cannot be compiled successfully.

The purpose of this extension is to familiarize yourself with ZEND APIs and understand how to provide PHP APIs.
Implemented PHP APIs include:
PHP_FUNCTION (my_mysql_connect );
PHP_FUNCTION (my_mysql_close );
PHP_FUNCTION (my_mysql_get_conn); // obtain mysql resources through the connection pool. This connection pool is simple and automatically resized, and does not contract.
PHP_FUNCTION (my_mysql_select_db); // switch the database
PHP_FUNCTION (my_mysql_ping );
PHP_FUNCTION (my_mysql_query); // execute the SQL statement. It can be a query statement with or without return values, such as SELECT, INSERT, UPDATE, and DELETE.
PHP_FUNCTION (my_mysql_fetch_assoc); // obtain the returned result set
PHP_FUNCTION (my_mysql_get_insert_id); // gets the auto-increment ID of the last INSERT operation.

Ii. Development Ideas
1. Use ext_skel_win32.php (skeleton tool) to create the my_mysql extension.
2. Use VS2008 to open my_myqsl.dsp and edit project properties.
2.1 switch to the Release version. The default version is DEBUG.
2.2 right-click Project properties-general configuration properties-C/C ++-pre-processor definition to remove ZTS = 1, because we will compile it into the ETS version. (Non-thread security)
2.3 right-click Project properties-general configuration properties-C/C ++-General-Add the include directory and add "MYSQL_ROOT \ include. Introduce the header file search path.
In this way, when # include "mysql. h" is used, the header file cannot be searched.
2.4 right-click the "Project Properties"> "wildcard properties"> "linker"> "general"> "additional library directory" and add "MYSQL_ROOT \ lib \ opt". The DLL to be linked during compilation and extension is included in the directory. (In addition, do not forget to introduce the php5cnt. lib path. This is required for any extension compilation)
2.5 right-click Project properties-general configuration properties-connector-input-add libmySQL. lib to dependencies, and set the DLL to be linked during compilation.
3. Create a New PHP resource type for my_mysql.
4. Implement the php api and provide it to the PHP interface.
5. Compile the extension, copy the generated php_my_mysql.dll to PHP_ROOT \ ext, modify php. ini, and add extension = ext \ php_my_mysql.dll.
6. Compile the PHP file, call the functions provided in the extension, and debug the file.

@ MYSQL_ROOT: the installation path of mysql.
@ PHP_ROOT: indicates the php path.

Iii. Start Encoding
Ext \ my_mysql \ php_my_mysql.h

Copy codeThe Code is 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 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
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. phpCopy codeThe Code is 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:

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.