php-src/ext/mysql/php_mysql.c

2085 lines
54 KiB
C
Raw Normal View History

2001-02-01 09:36:52 +00:00
/*
1999-04-21 21:26:10 +00:00
+----------------------------------------------------------------------+
2001-12-11 15:32:16 +00:00
| PHP Version 4 |
1999-04-21 21:26:10 +00:00
+----------------------------------------------------------------------+
2001-12-11 15:32:16 +00:00
| Copyright (c) 1997-2002 The PHP Group |
1999-04-21 21:26:10 +00:00
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
1999-07-16 13:13:16 +00:00
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
1999-07-16 13:13:16 +00:00
| 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. |
1999-04-21 21:26:10 +00:00
+----------------------------------------------------------------------+
1999-07-16 13:13:16 +00:00
| Authors: Zeev Suraski <zeev@zend.com> |
1999-04-21 21:26:10 +00:00
+----------------------------------------------------------------------+
1999-09-03 19:54:12 +00:00
*/
1999-04-21 21:26:10 +00:00
/* $Id$ */
/* TODO:
*
* ? Safe mode implementation
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
1999-04-21 21:26:10 +00:00
#include "php.h"
#include "php_globals.h"
1999-09-03 19:54:12 +00:00
#include "php_mysql.h"
1999-12-28 18:45:10 +00:00
#include "ext/standard/info.h"
#include "ext/standard/php_string.h"
1999-04-21 21:26:10 +00:00
2000-02-12 19:47:49 +00:00
#ifdef PHP_WIN32
1999-04-21 21:26:10 +00:00
#include <winsock.h>
#define signal(a, b) NULL
1999-04-21 21:26:10 +00:00
#else
#include "build-defs.h"
2000-02-20 07:27:46 +00:00
#if HAVE_SIGNAL_H
#include <signal.h>
#endif
1999-04-21 21:26:10 +00:00
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <netdb.h>
#include <netinet/in.h>
#endif
2001-12-23 14:37:24 +00:00
#ifndef HAVE_USHORT
#undef ushort
typedef unsigned short ushort;
#endif
1999-04-23 11:12:41 +00:00
/* True globals, no need for thread safety */
static int le_result, le_link, le_plink;
1999-04-23 11:12:41 +00:00
1999-04-21 21:26:10 +00:00
#include "php_ini.h"
#if HAVE_MYSQL_MYSQL_H
# include <mysql/mysql.h>
#else
# include <mysql.h>
#endif
2001-11-02 06:42:12 +00:00
#ifdef HAVE_MYSQL_REAL_CONNECT
# ifdef HAVE_ERRMSG_H
# include <errmsg.h>
# endif
1999-04-21 21:26:10 +00:00
#endif
#define SAFE_STRING(s) ((s)?(s):"")
#if MYSQL_VERSION_ID > 32199
#define mysql_row_length_type unsigned long
1999-08-17 22:06:55 +00:00
#define HAVE_MYSQL_ERRNO
1999-04-21 21:26:10 +00:00
#else
#define mysql_row_length_type unsigned int
1999-08-17 22:06:55 +00:00
# ifdef mysql_errno
# define HAVE_MYSQL_ERRNO
# endif
1999-04-21 21:26:10 +00:00
#endif
#if MYSQL_VERSION_ID >= 32032
#define HAVE_GETINFO_FUNCS
#endif
#if MYSQL_VERSION_ID > 32133 || defined(FIELD_TYPE_TINY)
#define MYSQL_HAS_TINY
#endif
2001-01-31 01:14:54 +00:00
#if MYSQL_VERSION_ID >= 32200
#define MYSQL_HAS_YEAR
#endif
#define MYSQL_ASSOC 1<<0
#define MYSQL_NUM 1<<1
#define MYSQL_BOTH (MYSQL_ASSOC|MYSQL_NUM)
1999-04-21 21:26:10 +00:00
#define MYSQL_USE_RESULT 0
#define MYSQL_STORE_RESULT 1
#if MYSQL_VERSION_ID < 32223
1999-09-06 05:21:45 +00:00
#define PHP_MYSQL_VALID_RESULT(mysql) \
(mysql_num_fields(mysql)>0)
#else
#define PHP_MYSQL_VALID_RESULT(mysql) \
(mysql_field_count(mysql)>0)
#endif
1999-09-06 05:21:45 +00:00
typedef struct _php_mysql_conn {
MYSQL conn;
int active_result_id;
} php_mysql_conn;
/* {{{ mysql_functions[]
*/
1999-04-21 21:26:10 +00:00
function_entry mysql_functions[] = {
1999-08-02 16:06:13 +00:00
PHP_FE(mysql_connect, NULL)
PHP_FE(mysql_pconnect, NULL)
PHP_FE(mysql_close, NULL)
PHP_FE(mysql_select_db, NULL)
2001-11-02 06:42:12 +00:00
#if MYSQL_VERSION_ID < 40000
1999-08-02 16:06:13 +00:00
PHP_FE(mysql_create_db, NULL)
PHP_FE(mysql_drop_db, NULL)
2001-11-02 06:42:12 +00:00
#endif
1999-08-02 16:06:13 +00:00
PHP_FE(mysql_query, NULL)
PHP_FE(mysql_unbuffered_query, NULL)
1999-08-02 16:06:13 +00:00
PHP_FE(mysql_db_query, NULL)
PHP_FE(mysql_list_dbs, NULL)
PHP_FE(mysql_list_tables, NULL)
PHP_FE(mysql_list_fields, NULL)
PHP_FE(mysql_error, NULL)
1999-08-17 22:06:55 +00:00
#ifdef HAVE_MYSQL_ERRNO
1999-08-02 16:06:13 +00:00
PHP_FE(mysql_errno, NULL)
1999-04-21 21:26:10 +00:00
#endif
1999-08-02 16:06:13 +00:00
PHP_FE(mysql_affected_rows, NULL)
PHP_FE(mysql_insert_id, NULL)
PHP_FE(mysql_result, NULL)
PHP_FE(mysql_num_rows, NULL)
PHP_FE(mysql_num_fields, NULL)
PHP_FE(mysql_fetch_row, NULL)
PHP_FE(mysql_fetch_array, NULL)
PHP_FE(mysql_fetch_assoc, NULL)
1999-08-02 16:06:13 +00:00
PHP_FE(mysql_fetch_object, NULL)
PHP_FE(mysql_data_seek, NULL)
PHP_FE(mysql_fetch_lengths, NULL)
PHP_FE(mysql_fetch_field, NULL)
PHP_FE(mysql_field_seek, NULL)
PHP_FE(mysql_free_result, NULL)
PHP_FE(mysql_field_name, NULL)
PHP_FE(mysql_field_table, NULL)
PHP_FE(mysql_field_len, NULL)
PHP_FE(mysql_field_type, NULL)
PHP_FE(mysql_field_flags, NULL)
2000-10-11 18:27:21 +00:00
PHP_FE(mysql_escape_string, NULL)
#ifdef HAVE_GETINFO_FUNCS
PHP_FE(mysql_get_client_info, NULL)
PHP_FE(mysql_get_host_info, NULL)
PHP_FE(mysql_get_proto_info, NULL)
PHP_FE(mysql_get_server_info, NULL)
#endif
1999-08-02 16:06:13 +00:00
1999-04-21 21:26:10 +00:00
/* for downwards compatability */
1999-08-02 16:06:13 +00:00
PHP_FALIAS(mysql, mysql_db_query, NULL)
PHP_FALIAS(mysql_fieldname, mysql_field_name, NULL)
PHP_FALIAS(mysql_fieldtable, mysql_field_table, NULL)
PHP_FALIAS(mysql_fieldlen, mysql_field_len, NULL)
PHP_FALIAS(mysql_fieldtype, mysql_field_type, NULL)
PHP_FALIAS(mysql_fieldflags, mysql_field_flags, NULL)
PHP_FALIAS(mysql_selectdb, mysql_select_db, NULL)
2001-11-02 06:42:12 +00:00
#if MYSQL_VERSION_ID < 40000
1999-08-02 16:06:13 +00:00
PHP_FALIAS(mysql_createdb, mysql_create_db, NULL)
PHP_FALIAS(mysql_dropdb, mysql_drop_db, NULL)
2001-11-02 06:42:12 +00:00
#endif
1999-08-02 16:06:13 +00:00
PHP_FALIAS(mysql_freeresult, mysql_free_result, NULL)
PHP_FALIAS(mysql_numfields, mysql_num_fields, NULL)
PHP_FALIAS(mysql_numrows, mysql_num_rows, NULL)
PHP_FALIAS(mysql_listdbs, mysql_list_dbs, NULL)
PHP_FALIAS(mysql_listtables, mysql_list_tables, NULL)
PHP_FALIAS(mysql_listfields, mysql_list_fields, NULL)
PHP_FALIAS(mysql_db_name, mysql_result, NULL)
PHP_FALIAS(mysql_dbname, mysql_result, NULL)
PHP_FALIAS(mysql_tablename, mysql_result, NULL)
1999-04-21 21:26:10 +00:00
{NULL, NULL, NULL}
};
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ mysql_module_entry
*/
1999-09-03 19:54:12 +00:00
zend_module_entry mysql_module_entry = {
STANDARD_MODULE_HEADER,
"mysql",
mysql_functions,
ZEND_MODULE_STARTUP_N(mysql),
PHP_MSHUTDOWN(mysql),
PHP_RINIT(mysql),
PHP_RSHUTDOWN(mysql),
PHP_MINFO(mysql),
NO_VERSION_YET,
STANDARD_MODULE_PROPERTIES
1999-04-21 21:26:10 +00:00
};
/* }}} */
ZEND_DECLARE_MODULE_GLOBALS(mysql)
1999-04-21 21:26:10 +00:00
2000-05-23 09:33:51 +00:00
#ifdef COMPILE_DL_MYSQL
ZEND_GET_MODULE(mysql)
1999-04-21 21:26:10 +00:00
#endif
void timeout(int sig);
1999-04-21 21:26:10 +00:00
#define CHECK_LINK(link) { if (link==-1) { php_error(E_WARNING, "MySQL: A link to the server could not be established"); RETURN_FALSE; } }
1999-04-21 21:26:10 +00:00
/* {{{ _free_mysql_result
* This wrapper is required since mysql_free_result() returns an integer, and
* thus, cannot be used directly
1999-04-21 21:26:10 +00:00
*/
2001-07-31 05:44:11 +00:00
static void _free_mysql_result(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
MYSQL_RES *mysql_result = (MYSQL_RES *)rsrc->ptr;
1999-04-21 21:26:10 +00:00
mysql_free_result(mysql_result);
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ php_mysql_set_default_link
*/
static void php_mysql_set_default_link(int id TSRMLS_DC)
1999-04-21 21:26:10 +00:00
{
if (MySG(default_link)!=-1) {
1999-09-03 19:54:12 +00:00
zend_list_delete(MySG(default_link));
1999-04-21 21:26:10 +00:00
}
MySG(default_link) = id;
zend_list_addref(id);
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ _close_mysql_link
*/
2001-07-31 05:44:11 +00:00
static void _close_mysql_link(zend_rsrc_list_entry *rsrc TSRMLS_DC)
1999-04-21 21:26:10 +00:00
{
php_mysql_conn *link = (php_mysql_conn *)rsrc->ptr;
void (*handler) (int);
1999-04-21 21:26:10 +00:00
handler = signal(SIGPIPE, SIG_IGN);
mysql_close(&link->conn);
signal(SIGPIPE, handler);
1999-04-21 21:26:10 +00:00
efree(link);
MySG(num_links)--;
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ _close_mysql_plink
*/
2001-07-31 05:44:11 +00:00
static void _close_mysql_plink(zend_rsrc_list_entry *rsrc TSRMLS_DC)
1999-04-21 21:26:10 +00:00
{
php_mysql_conn *link = (php_mysql_conn *)rsrc->ptr;
void (*handler) (int);
1999-04-21 21:26:10 +00:00
handler = signal(SIGPIPE, SIG_IGN);
mysql_close(&link->conn);
signal(SIGPIPE, handler);
1999-04-21 21:26:10 +00:00
free(link);
MySG(num_persistent)--;
MySG(num_links)--;
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ PHP_INI_MH
*/
1999-04-21 21:26:10 +00:00
static PHP_INI_MH(OnMySQLPort)
{
if (new_value==NULL) { /* default port */
2000-02-12 19:47:49 +00:00
#ifndef PHP_WIN32
1999-04-21 21:26:10 +00:00
struct servent *serv_ptr;
char *env;
MySG(default_port) = MYSQL_PORT;
if ((serv_ptr = getservbyname("mysql", "tcp"))) {
MySG(default_port) = (uint) ntohs((ushort) serv_ptr->s_port);
}
if ((env = getenv("MYSQL_TCP_PORT"))) {
MySG(default_port) = (uint) atoi(env);
}
#else
MySG(default_port) = MYSQL_PORT;
#endif
} else {
MySG(default_port) = atoi(new_value);
}
return SUCCESS;
}
/* }}} */
1999-04-21 21:26:10 +00:00
2001-06-05 16:06:31 +00:00
/* {{{ PHP_INI */
1999-04-21 21:26:10 +00:00
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("mysql.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateInt, allow_persistent, zend_mysql_globals, mysql_globals)
STD_PHP_INI_ENTRY_EX("mysql.max_persistent", "-1", PHP_INI_SYSTEM, OnUpdateInt, max_persistent, zend_mysql_globals, mysql_globals, display_link_numbers)
STD_PHP_INI_ENTRY_EX("mysql.max_links", "-1", PHP_INI_SYSTEM, OnUpdateInt, max_links, zend_mysql_globals, mysql_globals, display_link_numbers)
STD_PHP_INI_ENTRY("mysql.default_host", NULL, PHP_INI_ALL, OnUpdateString, default_host, zend_mysql_globals, mysql_globals)
STD_PHP_INI_ENTRY("mysql.default_user", NULL, PHP_INI_ALL, OnUpdateString, default_user, zend_mysql_globals, mysql_globals)
STD_PHP_INI_ENTRY("mysql.default_password", NULL, PHP_INI_ALL, OnUpdateString, default_password, zend_mysql_globals, mysql_globals)
PHP_INI_ENTRY("mysql.default_port", NULL, PHP_INI_ALL, OnMySQLPort)
STD_PHP_INI_ENTRY("mysql.default_socket", NULL, PHP_INI_ALL, OnUpdateStringUnempty, default_socket, zend_mysql_globals, mysql_globals)
1999-04-21 21:26:10 +00:00
PHP_INI_END()
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ php_mysql_init_globals
*/
static void php_mysql_init_globals(zend_mysql_globals *mysql_globals)
1999-04-23 11:12:41 +00:00
{
mysql_globals->num_persistent = 0;
mysql_globals->default_socket = NULL;
mysql_globals->default_host = NULL;
mysql_globals->default_user = NULL;
mysql_globals->default_password = NULL;
mysql_globals->connect_errno = 0;
mysql_globals->connect_error = NULL;
1999-04-23 11:12:41 +00:00
}
/* }}} */
1999-04-23 11:12:41 +00:00
/* {{{ PHP_MINIT_FUNCTION
*/
2001-08-11 16:39:07 +00:00
ZEND_MODULE_STARTUP_D(mysql)
1999-04-21 21:26:10 +00:00
{
ZEND_INIT_MODULE_GLOBALS(mysql, php_mysql_init_globals, NULL);
1999-04-23 11:12:41 +00:00
REGISTER_INI_ENTRIES();
le_result = zend_register_list_destructors_ex(_free_mysql_result, NULL, "mysql result", module_number);
le_link = zend_register_list_destructors_ex(_close_mysql_link, NULL, "mysql link", module_number);
le_plink = zend_register_list_destructors_ex(NULL, _close_mysql_plink, "mysql link persistent", module_number);
Z_TYPE(mysql_module_entry) = type;
REGISTER_LONG_CONSTANT("MYSQL_ASSOC", MYSQL_ASSOC, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MYSQL_NUM", MYSQL_NUM, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MYSQL_BOTH", MYSQL_BOTH, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MYSQL_USE_RESULT", MYSQL_USE_RESULT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MYSQL_STORE_RESULT", MYSQL_STORE_RESULT, CONST_CS | CONST_PERSISTENT);
1999-04-21 21:26:10 +00:00
2001-11-02 06:42:12 +00:00
#ifdef ZTS
# if MYSQL_VERSION_ID >= 40000
mysql_thread_init();
# endif
#endif
1999-04-21 21:26:10 +00:00
return SUCCESS;
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
1999-08-02 16:06:13 +00:00
PHP_MSHUTDOWN_FUNCTION(mysql)
1999-04-21 21:26:10 +00:00
{
2001-11-02 06:42:12 +00:00
#ifdef ZTS
# if MYSQL_VERSION_ID >= 40000
mysql_thread_end();
# endif
#endif
1999-04-21 21:26:10 +00:00
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ PHP_RINIT_FUNCTION
*/
1999-08-02 16:06:13 +00:00
PHP_RINIT_FUNCTION(mysql)
1999-04-21 21:26:10 +00:00
{
MySG(default_link)=-1;
MySG(num_links) = MySG(num_persistent);
/* Reset connect error/errno on every request */
MySG(connect_error) = NULL;
MySG(connect_errno)=0;
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(mysql)
{
if (MySG(connect_error)!=NULL) {
efree(MySG(connect_error));
}
1999-04-21 21:26:10 +00:00
return SUCCESS;
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ PHP_MINFO_FUNCTION
*/
1999-08-02 16:06:13 +00:00
PHP_MINFO_FUNCTION(mysql)
1999-04-21 21:26:10 +00:00
{
char buf[32];
2000-04-05 20:18:58 +00:00
php_info_print_table_start();
2000-04-06 21:07:44 +00:00
php_info_print_table_header(2, "MySQL Support", "enabled");
1999-08-14 10:04:04 +00:00
sprintf(buf, "%ld", MySG(num_persistent));
php_info_print_table_row(2, "Active Persistent Links", buf);
1999-08-14 10:04:04 +00:00
sprintf(buf, "%ld", MySG(num_links));
php_info_print_table_row(2, "Active Links", buf);
php_info_print_table_row(2, "Client API version", mysql_get_client_info());
2000-02-12 19:47:49 +00:00
#ifndef PHP_WIN32
php_info_print_table_row(2, "MYSQL_MODULE_TYPE", PHP_MYSQL_TYPE);
php_info_print_table_row(2, "MYSQL_SOCKET", MYSQL_UNIX_ADDR);
php_info_print_table_row(2, "MYSQL_INCLUDE", PHP_MYSQL_INCLUDE);
php_info_print_table_row(2, "MYSQL_LIBS", PHP_MYSQL_LIBS);
1999-04-21 21:26:10 +00:00
#endif
2000-04-05 20:18:58 +00:00
php_info_print_table_end();
2000-04-06 21:07:44 +00:00
DISPLAY_INI_ENTRIES();
1999-04-21 21:26:10 +00:00
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ php_mysql_do_connect
*/
#define MYSQL_DO_CONNECT_CLEANUP() \
if (free_host) { \
efree(host); \
}
#define MYSQL_DO_CONNECT_RETURN_FALSE() \
MYSQL_DO_CONNECT_CLEANUP(); \
RETURN_FALSE;
static void php_mysql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
1999-04-21 21:26:10 +00:00
{
char *user=NULL, *passwd=NULL, *host_and_port=NULL, *socket=NULL, *tmp=NULL, *host=NULL;
char *hashed_details=NULL;
int hashed_details_length, port = MYSQL_PORT;
php_mysql_conn *mysql=NULL;
void (*handler) (int);
zval **z_host=NULL, **z_user=NULL, **z_passwd=NULL, **z_new_link=NULL;
zend_bool free_host=0, new_link=0;
1999-04-21 21:26:10 +00:00
socket = MySG(default_socket);
1999-04-21 21:26:10 +00:00
if (PG(sql_safe_mode)) {
if (ZEND_NUM_ARGS()>0) {
php_error(E_NOTICE, "SQL safe mode in effect - ignoring host/user/password information");
1999-04-21 21:26:10 +00:00
}
host_and_port=passwd=NULL;
1999-12-17 19:16:50 +00:00
user=php_get_current_user();
1999-04-21 21:26:10 +00:00
hashed_details_length = strlen(user)+5+3;
hashed_details = (char *) emalloc(hashed_details_length+1);
sprintf(hashed_details, "mysql__%s_", user);
1999-04-21 21:26:10 +00:00
} else {
host_and_port = MySG(default_host);
1999-04-21 21:26:10 +00:00
user = MySG(default_user);
passwd = MySG(default_password);
switch(ZEND_NUM_ARGS()) {
1999-04-21 21:26:10 +00:00
case 0: /* defaults */
break;
case 1: {
if (zend_get_parameters_ex(1, &z_host)==FAILURE) {
MYSQL_DO_CONNECT_RETURN_FALSE();
1999-04-21 21:26:10 +00:00
}
}
break;
case 2: {
if (zend_get_parameters_ex(2, &z_host, &z_user)==FAILURE) {
MYSQL_DO_CONNECT_RETURN_FALSE();
1999-04-21 21:26:10 +00:00
}
convert_to_string_ex(z_user);
user = Z_STRVAL_PP(z_user);
1999-04-21 21:26:10 +00:00
}
break;
case 3: {
if (zend_get_parameters_ex(3, &z_host, &z_user, &z_passwd) == FAILURE) {
MYSQL_DO_CONNECT_RETURN_FALSE();
1999-04-21 21:26:10 +00:00
}
convert_to_string_ex(z_user);
convert_to_string_ex(z_passwd);
user = Z_STRVAL_PP(z_user);
passwd = Z_STRVAL_PP(z_passwd);
1999-04-21 21:26:10 +00:00
}
break;
case 4: {
if (zend_get_parameters_ex(4, &z_host, &z_user, &z_passwd, &z_new_link) == FAILURE) {
MYSQL_DO_CONNECT_RETURN_FALSE();
}
convert_to_string_ex(z_user);
convert_to_string_ex(z_passwd);
user = Z_STRVAL_PP(z_user);
passwd = Z_STRVAL_PP(z_passwd);
new_link = Z_BVAL_PP(z_new_link);
}
break;
1999-04-21 21:26:10 +00:00
default:
WRONG_PARAM_COUNT;
break;
}
if (z_host) {
SEPARATE_ZVAL(z_host); /* We may modify z_host if it contains a port, separate */
convert_to_string_ex(z_host);
host_and_port = Z_STRVAL_PP(z_host);
if (z_user) {
convert_to_string_ex(z_user);
user = Z_STRVAL_PP(z_user);
if (z_passwd) {
convert_to_string_ex(z_passwd);
passwd = Z_STRVAL_PP(z_passwd);
}
}
}
hashed_details_length = sizeof("mysql___")-1 + strlen(SAFE_STRING(host_and_port))+strlen(SAFE_STRING(user))+strlen(SAFE_STRING(passwd));
1999-04-21 21:26:10 +00:00
hashed_details = (char *) emalloc(hashed_details_length+1);
sprintf(hashed_details, "mysql_%s_%s_%s", SAFE_STRING(host_and_port), SAFE_STRING(user), SAFE_STRING(passwd));
1999-04-21 21:26:10 +00:00
}
/* We cannot use mysql_port anymore in windows, need to use
* mysql_real_connect() to set the port.
*/
if (host_and_port && (tmp=strchr(host_and_port, ':'))) {
host = estrndup(host_and_port, tmp-host_and_port);
free_host = 1;
1999-04-21 21:26:10 +00:00
tmp++;
if (tmp[0] != '/') {
port = atoi(tmp);
if ((tmp=strchr(tmp, ':'))) {
tmp++;
socket=tmp;
}
} else {
socket = tmp;
}
1999-04-21 21:26:10 +00:00
} else {
host = host_and_port;
1999-04-21 21:26:10 +00:00
port = MySG(default_port);
}
#if MYSQL_VERSION_ID < 32200
mysql_port = port;
#endif
if (!MySG(allow_persistent)) {
persistent=0;
}
if (persistent) {
list_entry *le;
/* try to find if we already have this link in our persistent list */
if (zend_hash_find(&EG(persistent_list), hashed_details, hashed_details_length+1, (void **) &le)==FAILURE) { /* we don't */
1999-04-21 21:26:10 +00:00
list_entry new_le;
if (MySG(max_links)!=-1 && MySG(num_links)>=MySG(max_links)) {
php_error(E_WARNING, "MySQL: Too many open links (%d)", MySG(num_links));
1999-04-21 21:26:10 +00:00
efree(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
1999-04-21 21:26:10 +00:00
}
if (MySG(max_persistent)!=-1 && MySG(num_persistent)>=MySG(max_persistent)) {
php_error(E_WARNING, "MySQL: Too many open persistent links (%d)", MySG(num_persistent));
1999-04-21 21:26:10 +00:00
efree(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
1999-04-21 21:26:10 +00:00
}
/* create the link */
2001-04-29 11:37:03 +00:00
mysql = (php_mysql_conn *) malloc(sizeof(php_mysql_conn));
mysql->active_result_id = 0;
1999-04-21 21:26:10 +00:00
#if MYSQL_VERSION_ID > 32199 /* this lets us set the port number */
2001-04-29 11:37:03 +00:00
mysql_init(&mysql->conn);
if (mysql_real_connect(&mysql->conn, host, user, passwd, NULL, port, socket, 0)==NULL) {
1999-04-21 21:26:10 +00:00
#else
2001-04-29 11:37:03 +00:00
if (mysql_connect(&mysql->conn, host, user, passwd)==NULL) {
1999-04-21 21:26:10 +00:00
#endif
/* Populate connect error globals so that the error functions can read them */
if (MySG(connect_error)!=NULL) efree(MySG(connect_error));
MySG(connect_error)=estrdup(mysql_error(&mysql->conn));
php_error(E_WARNING, "%s", MySG(connect_error));
#if defined(HAVE_MYSQL_ERRNO)
MySG(connect_errno)=mysql_errno(&mysql->conn);
#endif
1999-04-21 21:26:10 +00:00
free(mysql);
efree(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
1999-04-21 21:26:10 +00:00
}
/* hash it up */
Z_TYPE(new_le) = le_plink;
1999-04-21 21:26:10 +00:00
new_le.ptr = mysql;
2000-02-05 15:16:12 +00:00
if (zend_hash_update(&EG(persistent_list), hashed_details, hashed_details_length+1, (void *) &new_le, sizeof(list_entry), NULL)==FAILURE) {
1999-04-21 21:26:10 +00:00
free(mysql);
efree(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
1999-04-21 21:26:10 +00:00
}
MySG(num_persistent)++;
MySG(num_links)++;
} else { /* we do */
if (Z_TYPE_P(le) != le_plink) {
MYSQL_DO_CONNECT_RETURN_FALSE();
1999-04-21 21:26:10 +00:00
}
/* ensure that the link did not die */
handler=signal(SIGPIPE, SIG_IGN);
1999-08-17 22:06:55 +00:00
#if defined(HAVE_MYSQL_ERRNO) && defined(CR_SERVER_GONE_ERROR)
1999-04-21 21:26:10 +00:00
mysql_stat(le->ptr);
if (mysql_errno(&((php_mysql_conn *) le->ptr)->conn) == CR_SERVER_GONE_ERROR) {
1999-04-21 21:26:10 +00:00
#else
if (!strcasecmp(mysql_stat(le->ptr), "mysql server has gone away")) { /* the link died */
1999-04-21 21:26:10 +00:00
#endif
signal(SIGPIPE, handler);
1999-04-21 21:26:10 +00:00
#if MYSQL_VERSION_ID > 32199 /* this lets us set the port number */
if (mysql_real_connect(le->ptr, host, user, passwd, NULL, port, socket, 0)==NULL) {
1999-04-21 21:26:10 +00:00
#else
if (mysql_connect(le->ptr, host, user, passwd)==NULL) {
1999-04-21 21:26:10 +00:00
#endif
php_error(E_WARNING, "MySQL: Link to server lost, unable to reconnect");
2000-02-05 15:16:12 +00:00
zend_hash_del(&EG(persistent_list), hashed_details, hashed_details_length+1);
1999-04-21 21:26:10 +00:00
efree(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
1999-04-21 21:26:10 +00:00
}
}
signal(SIGPIPE, handler);
mysql = (php_mysql_conn *) le->ptr;
1999-04-21 21:26:10 +00:00
}
1999-09-03 19:54:12 +00:00
ZEND_REGISTER_RESOURCE(return_value, mysql, le_plink);
1999-04-21 21:26:10 +00:00
} else { /* non persistent */
list_entry *index_ptr, new_index_ptr;
1999-04-21 21:26:10 +00:00
/* first we check the hash for the hashed_details key. if it exists,
* it should point us to the right offset where the actual mysql link sits.
* if it doesn't, open a new mysql link, add it to the resource list,
* and add a pointer to it with hashed_details as the key.
*/
if (!new_link && zend_hash_find(&EG(regular_list), hashed_details, hashed_details_length+1,(void **) &index_ptr)==SUCCESS) {
int type, link;
1999-04-21 21:26:10 +00:00
void *ptr;
if (Z_TYPE_P(index_ptr) != le_index_ptr) {
MYSQL_DO_CONNECT_RETURN_FALSE();
1999-04-21 21:26:10 +00:00
}
link = (int) index_ptr->ptr;
1999-09-03 19:54:12 +00:00
ptr = zend_list_find(link,&type); /* check if the link is still there */
1999-04-23 11:12:41 +00:00
if (ptr && (type==le_link || type==le_plink)) {
1999-04-21 21:26:10 +00:00
zend_list_addref(link);
Z_LVAL_P(return_value) = link;
php_mysql_set_default_link(link TSRMLS_CC);
Z_TYPE_P(return_value) = IS_RESOURCE;
1999-04-21 21:26:10 +00:00
efree(hashed_details);
MYSQL_DO_CONNECT_CLEANUP();
1999-04-21 21:26:10 +00:00
return;
} else {
zend_hash_del(&EG(regular_list), hashed_details, hashed_details_length+1);
1999-04-21 21:26:10 +00:00
}
}
if (MySG(max_links)!=-1 && MySG(num_links)>=MySG(max_links)) {
php_error(E_WARNING, "MySQL: Too many open links (%d)", MySG(num_links));
1999-04-21 21:26:10 +00:00
efree(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
1999-04-21 21:26:10 +00:00
}
mysql = (php_mysql_conn *) emalloc(sizeof(php_mysql_conn));
mysql->active_result_id = 0;
1999-04-21 21:26:10 +00:00
#if MYSQL_VERSION_ID > 32199 /* this lets us set the port number */
mysql_init(&mysql->conn);
if (mysql_real_connect(&mysql->conn, host, user, passwd, NULL, port, socket, 0)==NULL) {
1999-04-21 21:26:10 +00:00
#else
if (mysql_connect(&mysql->conn, host, user, passwd)==NULL) {
#endif
/* Populate connect error globals so that the error functions can read them */
if (MySG(connect_error)!=NULL) efree(MySG(connect_error));
MySG(connect_error)=estrdup(mysql_error(&mysql->conn));
php_error(E_WARNING, "%s", MySG(connect_error));
#if defined(HAVE_MYSQL_ERRNO)
MySG(connect_errno)=mysql_errno(&mysql->conn);
1999-04-21 21:26:10 +00:00
#endif
php_error(E_WARNING, "MySQL Connection Failed: %s\n", mysql_error(&mysql->conn));
1999-04-21 21:26:10 +00:00
efree(hashed_details);
efree(mysql);
MYSQL_DO_CONNECT_RETURN_FALSE();
1999-04-21 21:26:10 +00:00
}
/* add it to the list */
1999-09-03 19:54:12 +00:00
ZEND_REGISTER_RESOURCE(return_value, mysql, le_link);
1999-04-21 21:26:10 +00:00
/* add it to the hash */
new_index_ptr.ptr = (void *) Z_LVAL_P(return_value);
Z_TYPE(new_index_ptr) = le_index_ptr;
if (zend_hash_update(&EG(regular_list), hashed_details, hashed_details_length+1,(void *) &new_index_ptr, sizeof(list_entry), NULL)==FAILURE) {
1999-04-21 21:26:10 +00:00
efree(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
1999-04-21 21:26:10 +00:00
}
MySG(num_links)++;
}
efree(hashed_details);
php_mysql_set_default_link(Z_LVAL_P(return_value) TSRMLS_CC);
MYSQL_DO_CONNECT_CLEANUP();
1999-04-21 21:26:10 +00:00
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ php_mysql_get_default_link
*/
static int php_mysql_get_default_link(INTERNAL_FUNCTION_PARAMETERS)
1999-04-21 21:26:10 +00:00
{
if (MySG(default_link)==-1) { /* no link opened yet, implicitly open one */
ht = 0;
1999-09-03 19:54:12 +00:00
php_mysql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1999-04-21 21:26:10 +00:00
}
return MySG(default_link);
}
/* }}} */
/* {{{ proto resource mysql_connect([string hostname[:port][:/path/to/socket]] [, string username] [, string password] [, bool new])
2001-11-08 22:05:56 +00:00
Opens a connection to a MySQL Server */
PHP_FUNCTION(mysql_connect)
1999-04-21 21:26:10 +00:00
{
1999-09-03 19:54:12 +00:00
php_mysql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1999-04-21 21:26:10 +00:00
}
/* }}} */
/* {{{ proto resource mysql_pconnect([string hostname[:port][:/path/to/socket]] [, string username] [, string password])
2001-11-08 22:05:56 +00:00
Opens a persistent connection to a MySQL Server */
PHP_FUNCTION(mysql_pconnect)
1999-04-21 21:26:10 +00:00
{
1999-09-03 19:54:12 +00:00
php_mysql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1999-04-21 21:26:10 +00:00
}
/* }}} */
/* {{{ proto bool mysql_close([int link_identifier])
1999-04-21 21:26:10 +00:00
Close a MySQL connection */
PHP_FUNCTION(mysql_close)
1999-04-21 21:26:10 +00:00
{
zval **mysql_link=NULL;
1999-09-03 19:13:37 +00:00
int id;
php_mysql_conn *mysql;
1999-04-21 21:26:10 +00:00
switch (ZEND_NUM_ARGS()) {
1999-04-21 21:26:10 +00:00
case 0:
id = MySG(default_link);
break;
case 1:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(1, &mysql_link)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
1999-09-03 19:13:37 +00:00
id = -1;
1999-04-21 21:26:10 +00:00
break;
default:
WRONG_PARAM_COUNT;
break;
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
if (id==-1) { /* explicit resource number */
zend_list_delete(Z_RESVAL_PP(mysql_link));
}
if (id!=-1
|| (mysql_link && Z_RESVAL_PP(mysql_link)==MySG(default_link))) {
2000-11-03 00:41:29 +00:00
zend_list_delete(MySG(default_link));
MySG(default_link) = -1;
2000-11-03 00:41:29 +00:00
}
1999-04-21 21:26:10 +00:00
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool mysql_select_db(string database_name [, int link_identifier])
2001-11-08 22:05:56 +00:00
Selects a MySQL database */
PHP_FUNCTION(mysql_select_db)
1999-04-21 21:26:10 +00:00
{
zval **db, **mysql_link;
1999-09-03 19:13:37 +00:00
int id;
php_mysql_conn *mysql;
1999-04-21 21:26:10 +00:00
switch(ZEND_NUM_ARGS()) {
1999-04-21 21:26:10 +00:00
case 1:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(1, &db)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1999-09-03 19:13:37 +00:00
CHECK_LINK(id);
1999-04-21 21:26:10 +00:00
break;
case 2:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(2, &db, &mysql_link)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
1999-09-03 19:13:37 +00:00
id = -1;
1999-04-21 21:26:10 +00:00
break;
default:
WRONG_PARAM_COUNT;
break;
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1999-04-21 21:26:10 +00:00
convert_to_string_ex(db);
1999-04-21 21:26:10 +00:00
if (mysql_select_db(&mysql->conn, Z_STRVAL_PP(db))!=0) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
} else {
RETURN_TRUE;
}
}
/* }}} */
#ifdef HAVE_GETINFO_FUNCS
2001-03-12 15:14:38 +00:00
/* {{{ proto string mysql_get_client_info(void)
Returns a string that represents the client library version */
PHP_FUNCTION(mysql_get_client_info)
{
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
RETURN_STRING(mysql_get_client_info(),1);
}
/* }}} */
/* {{{ proto string mysql_get_host_info([int link_identifier])
2001-01-31 23:35:37 +00:00
Returns a string describing the type of connection in use, including the server host name */
PHP_FUNCTION(mysql_get_host_info)
{
zval **mysql_link;
int id;
php_mysql_conn *mysql;
switch(ZEND_NUM_ARGS()) {
case 0:
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
break;
case 1:
if (zend_get_parameters_ex(1,&mysql_link)==FAILURE) {
RETURN_FALSE;
}
id = -1;
break;
default:
WRONG_PARAM_COUNT;
break;
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
RETURN_STRING(mysql_get_host_info(&mysql->conn),1);
}
/* }}} */
/* {{{ proto int mysql_get_proto_info([int link_identifier])
Returns the protocol version used by current connection */
PHP_FUNCTION(mysql_get_proto_info)
{
zval **mysql_link;
int id;
php_mysql_conn *mysql;
switch(ZEND_NUM_ARGS()) {
case 0:
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
break;
case 1:
if (zend_get_parameters_ex(1,&mysql_link)==FAILURE) {
RETURN_FALSE;
}
id = -1;
break;
default:
WRONG_PARAM_COUNT;
break;
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
RETURN_LONG(mysql_get_proto_info(&mysql->conn));
}
/* }}} */
/* {{{ proto string mysql_get_server_info([int link_identifier])
Returns a string that represents the server version number */
PHP_FUNCTION(mysql_get_server_info)
{
zval **mysql_link;
int id;
php_mysql_conn *mysql;
switch(ZEND_NUM_ARGS()) {
case 0:
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
break;
case 1:
if (zend_get_parameters_ex(1,&mysql_link)==FAILURE) {
RETURN_FALSE;
}
id = -1;
break;
default:
WRONG_PARAM_COUNT;
break;
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
RETURN_STRING(mysql_get_server_info(&mysql->conn),1);
}
/* }}} */
#endif
2001-11-02 06:42:12 +00:00
#if MYSQL_VERSION_ID < 40000
/* {{{ proto bool mysql_create_db(string database_name [, int link_identifier])
1999-04-21 21:26:10 +00:00
Create a MySQL database */
PHP_FUNCTION(mysql_create_db)
1999-04-21 21:26:10 +00:00
{
zval **db, **mysql_link;
1999-09-03 19:13:37 +00:00
int id;
php_mysql_conn *mysql;
1999-04-21 21:26:10 +00:00
switch(ZEND_NUM_ARGS()) {
1999-04-21 21:26:10 +00:00
case 1:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(1, &db)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1999-09-03 19:13:37 +00:00
CHECK_LINK(id);
1999-04-21 21:26:10 +00:00
break;
case 2:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(2, &db, &mysql_link)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
1999-09-03 19:13:37 +00:00
id = -1;
1999-04-21 21:26:10 +00:00
break;
default:
WRONG_PARAM_COUNT;
break;
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1999-04-21 21:26:10 +00:00
convert_to_string_ex(db);
if (mysql_create_db(&mysql->conn, Z_STRVAL_PP(db))==0) {
1999-04-21 21:26:10 +00:00
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto bool mysql_drop_db(string database_name [, int link_identifier])
2001-11-08 22:05:56 +00:00
Drops (delete) a MySQL database */
PHP_FUNCTION(mysql_drop_db)
1999-04-21 21:26:10 +00:00
{
zval **db, **mysql_link;
1999-09-03 19:13:37 +00:00
int id;
php_mysql_conn *mysql;
1999-04-21 21:26:10 +00:00
switch(ZEND_NUM_ARGS()) {
1999-04-21 21:26:10 +00:00
case 1:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(1, &db)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1999-09-03 19:13:37 +00:00
CHECK_LINK(id);
1999-04-21 21:26:10 +00:00
break;
case 2:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(2, &db, &mysql_link)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
1999-09-03 19:13:37 +00:00
id = -1;
1999-04-21 21:26:10 +00:00
break;
default:
WRONG_PARAM_COUNT;
break;
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1999-04-21 21:26:10 +00:00
convert_to_string_ex(db);
if (mysql_drop_db(&mysql->conn, Z_STRVAL_PP(db))==0) {
1999-04-21 21:26:10 +00:00
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
2001-11-02 06:42:12 +00:00
#endif
1999-04-21 21:26:10 +00:00
/* {{{ php_mysql_do_query_general
*/
static void php_mysql_do_query_general(zval **query, zval **mysql_link, int link_id, zval **db, int use_store, zval *return_value TSRMLS_DC)
2001-03-13 21:42:43 +00:00
{
php_mysql_conn *mysql;
MYSQL_RES *mysql_result;
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, link_id, "MySQL-Link", le_link, le_plink);
if (db) {
convert_to_string_ex(db);
if (mysql_select_db(&mysql->conn, Z_STRVAL_PP(db))!=0) {
2001-03-13 21:42:43 +00:00
RETURN_FALSE;
}
}
if (mysql->active_result_id) do {
int type;
MYSQL_RES *mysql_result;
mysql_result = (MYSQL_RES *) zend_list_find(mysql->active_result_id, &type);
if (mysql_result && type==le_result) {
if (!mysql_eof(mysql_result)) {
php_error(E_NOTICE, "Called %s() without first fetching all rows from a previous unbuffered query",
get_active_function_name(TSRMLS_C));
while (mysql_fetch_row(mysql_result));
}
zend_list_delete(mysql->active_result_id);
mysql->active_result_id = 0;
}
} while(0);
2001-03-13 21:42:43 +00:00
convert_to_string_ex(query);
/* mysql_query is binary unsafe, use mysql_real_query */
#if MYSQL_VERSION_ID > 32199
if (mysql_real_query(&mysql->conn, Z_STRVAL_PP(query), Z_STRLEN_PP(query))!=0) {
2001-03-13 21:42:43 +00:00
RETURN_FALSE;
}
#else
if (mysql_query(&mysql->conn, Z_STRVAL_PP(query))!=0) {
2001-03-13 21:42:43 +00:00
RETURN_FALSE;
}
#endif
if(use_store == MYSQL_USE_RESULT) {
mysql_result=mysql_use_result(&mysql->conn);
} else {
mysql_result=mysql_store_result(&mysql->conn);
}
if (!mysql_result) {
if (PHP_MYSQL_VALID_RESULT(&mysql->conn)) { /* query should have returned rows */
php_error(E_WARNING, "MySQL: Unable to save result set");
RETURN_FALSE;
} else {
RETURN_TRUE;
}
}
ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
if (use_store == MYSQL_USE_RESULT) {
mysql->active_result_id = Z_LVAL_P(return_value);
}
2001-03-13 21:42:43 +00:00
}
/* }}} */
2001-03-13 21:42:43 +00:00
/* {{{ php_mysql_do_query
*/
static void php_mysql_do_query(INTERNAL_FUNCTION_PARAMETERS, int use_store)
1999-04-21 21:26:10 +00:00
{
zval **query, **mysql_link;
int id;
1999-04-21 21:26:10 +00:00
switch(ZEND_NUM_ARGS()) {
1999-04-21 21:26:10 +00:00
case 1:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(1, &query)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1999-09-03 19:13:37 +00:00
CHECK_LINK(id);
1999-04-21 21:26:10 +00:00
break;
case 2:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(2, &query, &mysql_link)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
1999-09-03 19:13:37 +00:00
id = -1;
1999-04-21 21:26:10 +00:00
break;
default:
WRONG_PARAM_COUNT;
break;
}
php_mysql_do_query_general(query, mysql_link, id, NULL, use_store, return_value TSRMLS_CC);
}
/* }}} */
/* {{{ proto resource mysql_query(string query [, int link_identifier] [, int result_mode])
2001-11-08 22:05:56 +00:00
Sends an SQL query to MySQL */
PHP_FUNCTION(mysql_query)
{
php_mysql_do_query(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_STORE_RESULT);
}
/* }}} */
/* {{{ proto resource mysql_unbuffered_query(string query [, int link_identifier] [, int result_mode])
2001-11-08 22:05:56 +00:00
Sends an SQL query to MySQL, without fetching and buffering the result rows */
PHP_FUNCTION(mysql_unbuffered_query)
{
php_mysql_do_query(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_USE_RESULT);
1999-04-21 21:26:10 +00:00
}
/* }}} */
/* {{{ proto resource mysql_db_query(string database_name, string query [, int link_identifier])
2001-11-08 22:05:56 +00:00
Sends an SQL query to MySQL */
PHP_FUNCTION(mysql_db_query)
1999-04-21 21:26:10 +00:00
{
zval **db, **query, **mysql_link;
int id;
1999-04-21 21:26:10 +00:00
switch(ZEND_NUM_ARGS()) {
1999-04-21 21:26:10 +00:00
case 2:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(2, &db, &query)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1999-09-03 19:13:37 +00:00
CHECK_LINK(id);
1999-04-21 21:26:10 +00:00
break;
case 3:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(3, &db, &query, &mysql_link)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
1999-09-03 19:13:37 +00:00
id = -1;
1999-04-21 21:26:10 +00:00
break;
default:
WRONG_PARAM_COUNT;
break;
}
2001-07-30 08:24:42 +00:00
zend_error(E_NOTICE, "%s is deprecated; use mysql_select_db() and mysql_query() instead", get_active_function_name(TSRMLS_C));
php_mysql_do_query_general(query, mysql_link, id, db, MYSQL_STORE_RESULT, return_value TSRMLS_CC);
1999-04-21 21:26:10 +00:00
}
/* }}} */
/* {{{ proto resource mysql_list_dbs([int link_identifier])
1999-04-21 21:26:10 +00:00
List databases available on a MySQL server */
PHP_FUNCTION(mysql_list_dbs)
1999-04-21 21:26:10 +00:00
{
zval **mysql_link;
1999-09-03 19:13:37 +00:00
int id;
php_mysql_conn *mysql;
1999-04-21 21:26:10 +00:00
MYSQL_RES *mysql_result;
switch(ZEND_NUM_ARGS()) {
1999-04-21 21:26:10 +00:00
case 0:
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1999-09-03 19:13:37 +00:00
CHECK_LINK(id);
1999-04-21 21:26:10 +00:00
break;
case 1:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(1, &mysql_link)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
1999-09-03 19:13:37 +00:00
id = -1;
1999-04-21 21:26:10 +00:00
break;
default:
WRONG_PARAM_COUNT;
break;
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1999-09-03 19:13:37 +00:00
if ((mysql_result=mysql_list_dbs(&mysql->conn, NULL))==NULL) {
php_error(E_WARNING, "Unable to save MySQL query result");
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
1999-09-03 19:54:12 +00:00
ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
1999-04-21 21:26:10 +00:00
}
/* }}} */
/* {{{ proto resource mysql_list_tables(string database_name [, int link_identifier])
1999-04-21 21:26:10 +00:00
List tables in a MySQL database */
PHP_FUNCTION(mysql_list_tables)
1999-04-21 21:26:10 +00:00
{
zval **db, **mysql_link;
1999-09-03 19:13:37 +00:00
int id;
php_mysql_conn *mysql;
1999-04-21 21:26:10 +00:00
MYSQL_RES *mysql_result;
switch(ZEND_NUM_ARGS()) {
1999-04-21 21:26:10 +00:00
case 1:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(1, &db)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1999-09-03 19:13:37 +00:00
CHECK_LINK(id);
1999-04-21 21:26:10 +00:00
break;
case 2:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(2, &db, &mysql_link)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
1999-09-03 19:13:37 +00:00
id = -1;
1999-04-21 21:26:10 +00:00
break;
default:
WRONG_PARAM_COUNT;
break;
}
1999-09-03 19:13:37 +00:00
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1999-04-21 21:26:10 +00:00
convert_to_string_ex(db);
if (mysql_select_db(&mysql->conn, Z_STRVAL_PP(db))!=0) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
if ((mysql_result=mysql_list_tables(&mysql->conn, NULL))==NULL) {
php_error(E_WARNING, "Unable to save MySQL query result");
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
1999-09-03 19:54:12 +00:00
ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
1999-04-21 21:26:10 +00:00
}
/* }}} */
/* {{{ proto resource mysql_list_fields(string database_name, string table_name [, int link_identifier])
1999-04-21 21:26:10 +00:00
List MySQL result fields */
PHP_FUNCTION(mysql_list_fields)
1999-04-21 21:26:10 +00:00
{
zval **db, **table, **mysql_link;
1999-09-03 19:13:37 +00:00
int id;
php_mysql_conn *mysql;
1999-04-21 21:26:10 +00:00
MYSQL_RES *mysql_result;
switch(ZEND_NUM_ARGS()) {
1999-04-21 21:26:10 +00:00
case 2:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(2, &db, &table)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1999-09-03 19:13:37 +00:00
CHECK_LINK(id);
1999-04-21 21:26:10 +00:00
break;
case 3:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(3, &db, &table, &mysql_link)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
1999-09-03 19:13:37 +00:00
id = -1;
1999-04-21 21:26:10 +00:00
break;
default:
WRONG_PARAM_COUNT;
break;
}
1999-09-03 19:13:37 +00:00
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1999-04-21 21:26:10 +00:00
convert_to_string_ex(db);
if (mysql_select_db(&mysql->conn, Z_STRVAL_PP(db))!=0) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
convert_to_string_ex(table);
if ((mysql_result=mysql_list_fields(&mysql->conn, Z_STRVAL_PP(table), NULL))==NULL) {
php_error(E_WARNING, "Unable to save MySQL query result");
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
1999-09-03 19:54:12 +00:00
ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
1999-04-21 21:26:10 +00:00
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ proto string mysql_error([int link_identifier])
Returns the text of the error message from previous MySQL operation */
PHP_FUNCTION(mysql_error)
1999-04-21 21:26:10 +00:00
{
zval **mysql_link;
1999-09-03 19:13:37 +00:00
int id;
php_mysql_conn *mysql;
1999-04-21 21:26:10 +00:00
switch(ZEND_NUM_ARGS()) {
1999-04-21 21:26:10 +00:00
case 0:
id = MySG(default_link);
1999-09-03 19:13:37 +00:00
if (id==-1) {
if (MySG(connect_error)!=NULL){
RETURN_STRING(MySG(connect_error),1);
} else {
RETURN_FALSE;
}
1999-09-03 19:13:37 +00:00
}
1999-04-21 21:26:10 +00:00
break;
case 1:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(1, &mysql_link)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
1999-09-03 19:13:37 +00:00
id = -1;
1999-04-21 21:26:10 +00:00
break;
default:
WRONG_PARAM_COUNT;
break;
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1999-04-21 21:26:10 +00:00
RETURN_STRING(mysql_error(&mysql->conn), 1);
1999-04-21 21:26:10 +00:00
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ proto int mysql_errno([int link_identifier])
Returns the number of the error message from previous MySQL operation */
1999-08-17 22:06:55 +00:00
#ifdef HAVE_MYSQL_ERRNO
PHP_FUNCTION(mysql_errno)
1999-04-21 21:26:10 +00:00
{
zval **mysql_link;
1999-09-03 19:13:37 +00:00
int id;
php_mysql_conn *mysql;
1999-04-21 21:26:10 +00:00
switch(ZEND_NUM_ARGS()) {
1999-04-21 21:26:10 +00:00
case 0:
id = MySG(default_link);
1999-09-03 19:13:37 +00:00
if (id==-1) {
if (MySG(connect_errno)!=0){
RETURN_LONG(MySG(connect_errno));
} else {
RETURN_FALSE;
}
1999-09-03 19:13:37 +00:00
}
1999-04-21 21:26:10 +00:00
break;
case 1:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(1, &mysql_link)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
1999-09-03 19:13:37 +00:00
id = -1;
1999-04-21 21:26:10 +00:00
break;
default:
WRONG_PARAM_COUNT;
break;
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1999-04-21 21:26:10 +00:00
RETURN_LONG(mysql_errno(&mysql->conn));
1999-04-21 21:26:10 +00:00
}
#endif
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ proto int mysql_affected_rows([int link_identifier])
2001-11-08 22:05:56 +00:00
Gets number of affected rows in previous MySQL operation */
PHP_FUNCTION(mysql_affected_rows)
1999-04-21 21:26:10 +00:00
{
zval **mysql_link;
1999-09-03 19:13:37 +00:00
int id;
php_mysql_conn *mysql;
1999-04-21 21:26:10 +00:00
switch(ZEND_NUM_ARGS()) {
1999-04-21 21:26:10 +00:00
case 0:
id = MySG(default_link);
1999-09-03 19:13:37 +00:00
CHECK_LINK(id);
1999-04-21 21:26:10 +00:00
break;
case 1:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(1, &mysql_link)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
1999-09-03 19:13:37 +00:00
id = -1;
1999-04-21 21:26:10 +00:00
break;
default:
WRONG_PARAM_COUNT;
break;
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1999-04-21 21:26:10 +00:00
/* conversion from int64 to long happing here */
Z_LVAL_P(return_value) = (long) mysql_affected_rows(&mysql->conn);
Z_TYPE_P(return_value) = IS_LONG;
1999-04-21 21:26:10 +00:00
}
/* }}} */
2001-08-20 20:50:54 +00:00
/* {{{ proto string mysql_escape_string(string to_be_escaped)
2000-10-11 18:27:21 +00:00
Escape string for mysql query */
PHP_FUNCTION(mysql_escape_string)
{
zval **str;
if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &str) == FAILURE) {
ZEND_WRONG_PARAM_COUNT();
}
convert_to_string_ex(str);
/* assume worst case situation, which is 2x of the original string.
* we don't realloc() down to the real size since it'd most probably not
* be worth it
*/
Z_STRVAL_P(return_value) = (char *) emalloc(Z_STRLEN_PP(str)*2+1);
Z_STRLEN_P(return_value) = mysql_escape_string(Z_STRVAL_P(return_value), Z_STRVAL_PP(str), Z_STRLEN_PP(str));
Z_TYPE_P(return_value) = IS_STRING;
2000-10-11 18:27:21 +00:00
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ proto int mysql_insert_id([int link_identifier])
2001-11-08 22:05:56 +00:00
Gets the ID generated from the previous INSERT operation */
PHP_FUNCTION(mysql_insert_id)
1999-04-21 21:26:10 +00:00
{
zval **mysql_link;
1999-09-03 19:13:37 +00:00
int id;
php_mysql_conn *mysql;
1999-04-21 21:26:10 +00:00
switch(ZEND_NUM_ARGS()) {
1999-04-21 21:26:10 +00:00
case 0:
id = MySG(default_link);
1999-09-03 19:13:37 +00:00
CHECK_LINK(id);
1999-04-21 21:26:10 +00:00
break;
case 1:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(1, &mysql_link)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
1999-09-03 19:13:37 +00:00
id = -1;
1999-04-21 21:26:10 +00:00
break;
default:
WRONG_PARAM_COUNT;
break;
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, mysql_link, id, "MySQL-Link", le_link, le_plink);
1999-04-21 21:26:10 +00:00
/* conversion from int64 to long happing here */
Z_LVAL_P(return_value) = (long) mysql_insert_id(&mysql->conn);
Z_TYPE_P(return_value) = IS_LONG;
1999-04-21 21:26:10 +00:00
}
/* }}} */
/* {{{ proto mixed mysql_result(int result, int row [, mixed field])
2001-11-08 22:05:56 +00:00
Gets result data */
PHP_FUNCTION(mysql_result)
1999-04-21 21:26:10 +00:00
{
zval **result, **row, **field=NULL;
1999-04-21 21:26:10 +00:00
MYSQL_RES *mysql_result;
MYSQL_ROW sql_row;
mysql_row_length_type *sql_row_lengths;
1999-09-03 19:13:37 +00:00
int field_offset=0;
1999-04-21 21:26:10 +00:00
switch (ZEND_NUM_ARGS()) {
1999-04-21 21:26:10 +00:00
case 2:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(2, &result, &row)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
break;
case 3:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(3, &result, &row, &field)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
break;
default:
WRONG_PARAM_COUNT;
break;
}
1999-09-03 19:13:37 +00:00
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
convert_to_long_ex(row);
if (Z_LVAL_PP(row)<0 || Z_LVAL_PP(row)>=(int)mysql_num_rows(mysql_result)) {
php_error(E_WARNING, "Unable to jump to row %d on MySQL result index %d", Z_LVAL_PP(row), Z_LVAL_PP(result));
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
mysql_data_seek(mysql_result, Z_LVAL_PP(row));
1999-04-21 21:26:10 +00:00
if ((sql_row=mysql_fetch_row(mysql_result))==NULL
|| (sql_row_lengths=mysql_fetch_lengths(mysql_result))==NULL) { /* shouldn't happen? */
RETURN_FALSE;
}
if (field) {
switch(Z_TYPE_PP(field)) {
1999-04-21 21:26:10 +00:00
case IS_STRING: {
int i=0;
MYSQL_FIELD *tmp_field;
char *table_name, *field_name, *tmp;
1999-04-21 21:26:10 +00:00
if ((tmp=strchr(Z_STRVAL_PP(field), '.'))) {
table_name = estrndup(Z_STRVAL_PP(field), tmp-Z_STRVAL_PP(field));
1999-04-21 21:26:10 +00:00
field_name = estrdup(tmp+1);
} else {
table_name = NULL;
field_name = estrndup(Z_STRVAL_PP(field),Z_STRLEN_PP(field));
1999-04-21 21:26:10 +00:00
}
mysql_field_seek(mysql_result, 0);
1999-04-21 21:26:10 +00:00
while ((tmp_field=mysql_fetch_field(mysql_result))) {
if ((!table_name || !strcasecmp(tmp_field->table, table_name)) && !strcasecmp(tmp_field->name, field_name)) {
1999-04-21 21:26:10 +00:00
field_offset = i;
break;
}
i++;
}
if (!tmp_field) { /* no match found */
php_error(E_WARNING, "%s%s%s not found in MySQL result index %d",
(table_name?table_name:""), (table_name?".":""), field_name, Z_LVAL_PP(result));
1999-04-21 21:26:10 +00:00
efree(field_name);
if (table_name) {
efree(table_name);
}
RETURN_FALSE;
}
efree(field_name);
if (table_name) {
efree(table_name);
}
}
break;
default:
convert_to_long_ex(field);
field_offset = Z_LVAL_PP(field);
1999-04-21 21:26:10 +00:00
if (field_offset<0 || field_offset>=(int)mysql_num_fields(mysql_result)) {
php_error(E_WARNING, "Bad column offset specified");
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
break;
}
}
if (sql_row[field_offset]) {
Z_TYPE_P(return_value) = IS_STRING;
2000-01-01 16:54:55 +00:00
1999-04-21 21:26:10 +00:00
if (PG(magic_quotes_runtime)) {
Z_STRVAL_P(return_value) = php_addslashes(sql_row[field_offset], sql_row_lengths[field_offset],&Z_STRLEN_P(return_value), 0 TSRMLS_CC);
1999-04-21 21:26:10 +00:00
} else {
Z_STRLEN_P(return_value) = sql_row_lengths[field_offset];
Z_STRVAL_P(return_value) = (char *) safe_estrndup(sql_row[field_offset], Z_STRLEN_P(return_value));
1999-04-21 21:26:10 +00:00
}
} else {
Z_TYPE_P(return_value) = IS_NULL;
1999-04-21 21:26:10 +00:00
}
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ proto int mysql_num_rows(int result)
2001-11-08 22:05:56 +00:00
Gets number of rows in a result */
PHP_FUNCTION(mysql_num_rows)
1999-04-21 21:26:10 +00:00
{
zval **result;
1999-04-21 21:26:10 +00:00
MYSQL_RES *mysql_result;
if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &result)==FAILURE) {
1999-04-21 21:26:10 +00:00
WRONG_PARAM_COUNT;
}
1999-09-03 19:13:37 +00:00
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
1999-04-21 21:26:10 +00:00
/* conversion from int64 to long happing here */
Z_LVAL_P(return_value) = (long) mysql_num_rows(mysql_result);
Z_TYPE_P(return_value) = IS_LONG;
1999-04-21 21:26:10 +00:00
}
/* }}} */
/* {{{ proto int mysql_num_fields(int result)
2001-11-08 22:05:56 +00:00
Gets number of fields in a result */
PHP_FUNCTION(mysql_num_fields)
1999-04-21 21:26:10 +00:00
{
zval **result;
1999-04-21 21:26:10 +00:00
MYSQL_RES *mysql_result;
if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &result)==FAILURE) {
1999-04-21 21:26:10 +00:00
WRONG_PARAM_COUNT;
}
1999-09-03 19:13:37 +00:00
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
1999-04-21 21:26:10 +00:00
Z_LVAL_P(return_value) = mysql_num_fields(mysql_result);
Z_TYPE_P(return_value) = IS_LONG;
1999-04-21 21:26:10 +00:00
}
/* }}} */
/* {{{ php_mysql_fetch_hash
*/
static void php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int result_type, int expected_args)
1999-04-21 21:26:10 +00:00
{
zval **result, **arg2;
1999-04-21 21:26:10 +00:00
MYSQL_RES *mysql_result;
MYSQL_ROW mysql_row;
MYSQL_FIELD *mysql_field;
mysql_row_length_type *mysql_row_lengths;
int num_fields;
int i;
if (ZEND_NUM_ARGS() > expected_args) {
WRONG_PARAM_COUNT;
}
switch (ZEND_NUM_ARGS()) {
case 1:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(1, &result)==FAILURE) {
RETURN_FALSE;
}
if (!result_type) {
result_type = MYSQL_BOTH;
}
break;
case 2:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(2, &result, &arg2)==FAILURE) {
RETURN_FALSE;
}
convert_to_long_ex(arg2);
result_type = Z_LVAL_PP(arg2);
break;
default:
WRONG_PARAM_COUNT;
break;
1999-04-21 21:26:10 +00:00
}
1999-09-03 19:13:37 +00:00
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
1999-04-21 21:26:10 +00:00
if ((mysql_row=mysql_fetch_row(mysql_result))==NULL
|| (mysql_row_lengths=mysql_fetch_lengths(mysql_result))==NULL) {
RETURN_FALSE;
}
num_fields = mysql_num_fields(mysql_result);
if (array_init(return_value)==FAILURE) {
RETURN_FALSE;
}
mysql_field_seek(mysql_result, 0);
for (mysql_field=mysql_fetch_field(mysql_result), i=0; mysql_field; mysql_field=mysql_fetch_field(mysql_result), i++) {
1999-04-21 21:26:10 +00:00
if (mysql_row[i]) {
1999-07-16 17:19:11 +00:00
char *data;
int data_len;
int should_copy;
1999-04-21 21:26:10 +00:00
if (PG(magic_quotes_runtime)) {
2001-08-06 03:50:52 +00:00
data = php_addslashes(mysql_row[i], mysql_row_lengths[i],&data_len, 0 TSRMLS_CC);
should_copy = 0;
1999-04-21 21:26:10 +00:00
} else {
data = mysql_row[i];
data_len = mysql_row_lengths[i];
should_copy = 1;
}
if (result_type & MYSQL_NUM) {
add_index_stringl(return_value, i, data, data_len, should_copy);
should_copy = 1;
}
if (result_type & MYSQL_ASSOC) {
add_assoc_stringl(return_value, mysql_field->name, data, data_len, should_copy);
1999-04-21 21:26:10 +00:00
}
} else {
/* NULL value. */
if (result_type & MYSQL_NUM)
add_index_null(return_value, i);
else
add_assoc_null(return_value, mysql_field->name);
1999-04-21 21:26:10 +00:00
}
}
}
/* }}} */
/* {{{ proto array mysql_fetch_row(int result)
2001-11-08 22:05:56 +00:00
Gets a result row as an enumerated array */
PHP_FUNCTION(mysql_fetch_row)
{
php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_NUM, 1);
}
/* }}} */
1999-07-19 20:09:05 +00:00
/* {{{ proto object mysql_fetch_object(int result [, int result_type])
1999-04-21 21:26:10 +00:00
Fetch a result row as an object */
PHP_FUNCTION(mysql_fetch_object)
1999-04-21 21:26:10 +00:00
{
php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_ASSOC, 2);
2001-07-28 19:02:53 +00:00
if (Z_TYPE_P(return_value) == IS_ARRAY) {
object_and_properties_init(return_value, &zend_standard_class_def, Z_ARRVAL_P(return_value));
1999-04-21 21:26:10 +00:00
}
}
/* }}} */
1999-07-19 20:09:05 +00:00
/* {{{ proto array mysql_fetch_array(int result [, int result_type])
2001-11-08 22:05:56 +00:00
Fetch a result row as an array (associative, numeric or both) */
PHP_FUNCTION(mysql_fetch_array)
1999-04-21 21:26:10 +00:00
{
php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 2);
1999-04-21 21:26:10 +00:00
}
/* }}} */
/* {{{ proto array mysql_fetch_assoc(int result)
Fetch a result row as an associative array */
PHP_FUNCTION(mysql_fetch_assoc)
{
php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_ASSOC, 1);
}
/* }}} */
/* {{{ proto bool mysql_data_seek(int result, int row_number)
1999-04-21 21:26:10 +00:00
Move internal result pointer */
PHP_FUNCTION(mysql_data_seek)
1999-04-21 21:26:10 +00:00
{
zval **result, **offset;
1999-04-21 21:26:10 +00:00
MYSQL_RES *mysql_result;
if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &result, &offset)==FAILURE) {
1999-04-21 21:26:10 +00:00
WRONG_PARAM_COUNT;
}
1999-09-03 19:13:37 +00:00
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
convert_to_long_ex(offset);
if (Z_LVAL_PP(offset)<0 || Z_LVAL_PP(offset)>=(int)mysql_num_rows(mysql_result)) {
php_error(E_WARNING, "Offset %d is invalid for MySQL result index %d", Z_LVAL_PP(offset), Z_LVAL_PP(result));
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
mysql_data_seek(mysql_result, Z_LVAL_PP(offset));
1999-04-21 21:26:10 +00:00
RETURN_TRUE;
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ proto array mysql_fetch_lengths(int result)
2001-11-08 22:05:56 +00:00
Gets max data size of each column in a result */
PHP_FUNCTION(mysql_fetch_lengths)
1999-04-21 21:26:10 +00:00
{
zval **result;
1999-04-21 21:26:10 +00:00
MYSQL_RES *mysql_result;
mysql_row_length_type *lengths;
int num_fields;
int i;
if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &result)==FAILURE) {
1999-04-21 21:26:10 +00:00
WRONG_PARAM_COUNT;
}
1999-09-03 19:13:37 +00:00
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
1999-04-21 21:26:10 +00:00
if ((lengths=mysql_fetch_lengths(mysql_result))==NULL) {
RETURN_FALSE;
}
if (array_init(return_value)==FAILURE) {
RETURN_FALSE;
}
num_fields = mysql_num_fields(mysql_result);
for (i=0; i<num_fields; i++) {
add_index_long(return_value, i, lengths[i]);
}
}
/* }}} */
/* {{{ php_mysql_get_field_name
*/
1999-09-03 19:54:12 +00:00
static char *php_mysql_get_field_name(int field_type)
1999-04-21 21:26:10 +00:00
{
switch(field_type) {
case FIELD_TYPE_STRING:
case FIELD_TYPE_VAR_STRING:
return "string";
break;
#ifdef MYSQL_HAS_TINY
1999-04-21 21:26:10 +00:00
case FIELD_TYPE_TINY:
#endif
case FIELD_TYPE_SHORT:
case FIELD_TYPE_LONG:
case FIELD_TYPE_LONGLONG:
case FIELD_TYPE_INT24:
return "int";
break;
case FIELD_TYPE_FLOAT:
case FIELD_TYPE_DOUBLE:
case FIELD_TYPE_DECIMAL:
return "real";
break;
case FIELD_TYPE_TIMESTAMP:
return "timestamp";
break;
#ifdef MYSQL_HAS_YEAR
case FIELD_TYPE_YEAR:
return "year";
break;
#endif
1999-04-21 21:26:10 +00:00
case FIELD_TYPE_DATE:
return "date";
break;
case FIELD_TYPE_TIME:
return "time";
break;
case FIELD_TYPE_DATETIME:
return "datetime";
break;
case FIELD_TYPE_TINY_BLOB:
case FIELD_TYPE_MEDIUM_BLOB:
case FIELD_TYPE_LONG_BLOB:
case FIELD_TYPE_BLOB:
return "blob";
break;
case FIELD_TYPE_NULL:
return "null";
break;
default:
return "unknown";
break;
}
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ proto object mysql_fetch_field(int result [, int field_offset])
2001-11-08 22:05:56 +00:00
Gets column information from a result and return as an object */
PHP_FUNCTION(mysql_fetch_field)
1999-04-21 21:26:10 +00:00
{
zval **result, **field=NULL;
1999-04-21 21:26:10 +00:00
MYSQL_RES *mysql_result;
MYSQL_FIELD *mysql_field;
switch (ZEND_NUM_ARGS()) {
1999-04-21 21:26:10 +00:00
case 1:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(1, &result)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
break;
case 2:
1999-12-18 22:40:35 +00:00
if (zend_get_parameters_ex(2, &result, &field)==FAILURE) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
convert_to_long_ex(field);
1999-04-21 21:26:10 +00:00
break;
default:
WRONG_PARAM_COUNT;
}
1999-09-03 19:13:37 +00:00
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
1999-04-21 21:26:10 +00:00
if (field) {
if (Z_LVAL_PP(field)<0 || Z_LVAL_PP(field)>=(int)mysql_num_fields(mysql_result)) {
php_error(E_WARNING, "MySQL: Bad field offset");
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
mysql_field_seek(mysql_result, Z_LVAL_PP(field));
1999-04-21 21:26:10 +00:00
}
if ((mysql_field=mysql_fetch_field(mysql_result))==NULL) {
RETURN_FALSE;
}
if (object_init(return_value)==FAILURE) {
RETURN_FALSE;
}
add_property_string(return_value, "name",(mysql_field->name?mysql_field->name:empty_string), 1);
add_property_string(return_value, "table",(mysql_field->table?mysql_field->table:empty_string), 1);
add_property_string(return_value, "def",(mysql_field->def?mysql_field->def:empty_string), 1);
add_property_long(return_value, "max_length", mysql_field->max_length);
add_property_long(return_value, "not_null", IS_NOT_NULL(mysql_field->flags)?1:0);
add_property_long(return_value, "primary_key", IS_PRI_KEY(mysql_field->flags)?1:0);
1999-04-21 21:26:10 +00:00
add_property_long(return_value, "multiple_key",(mysql_field->flags&MULTIPLE_KEY_FLAG?1:0));
add_property_long(return_value, "unique_key",(mysql_field->flags&UNIQUE_KEY_FLAG?1:0));
add_property_long(return_value, "numeric", IS_NUM(Z_TYPE_P(mysql_field))?1:0);
add_property_long(return_value, "blob", IS_BLOB(mysql_field->flags)?1:0);
add_property_string(return_value, "type", php_mysql_get_field_name(Z_TYPE_P(mysql_field)), 1);
1999-04-21 21:26:10 +00:00
add_property_long(return_value, "unsigned",(mysql_field->flags&UNSIGNED_FLAG?1:0));
add_property_long(return_value, "zerofill",(mysql_field->flags&ZEROFILL_FLAG?1:0));
}
/* }}} */
/* {{{ proto bool mysql_field_seek(int result, int field_offset)
2001-11-08 22:05:56 +00:00
Sets result pointer to a specific field offset */
PHP_FUNCTION(mysql_field_seek)
1999-04-21 21:26:10 +00:00
{
zval **result, **offset;
1999-04-21 21:26:10 +00:00
MYSQL_RES *mysql_result;
if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &result, &offset)==FAILURE) {
1999-04-21 21:26:10 +00:00
WRONG_PARAM_COUNT;
}
1999-09-03 19:13:37 +00:00
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
convert_to_long_ex(offset);
if (Z_LVAL_PP(offset)<0 || Z_LVAL_PP(offset)>=(int)mysql_num_fields(mysql_result)) {
php_error(E_WARNING, "Field %d is invalid for MySQL result index %d", Z_LVAL_PP(offset), Z_LVAL_PP(result));
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
mysql_field_seek(mysql_result, Z_LVAL_PP(offset));
1999-04-21 21:26:10 +00:00
RETURN_TRUE;
}
/* }}} */
#define PHP_MYSQL_FIELD_NAME 1
#define PHP_MYSQL_FIELD_TABLE 2
#define PHP_MYSQL_FIELD_LEN 3
#define PHP_MYSQL_FIELD_TYPE 4
#define PHP_MYSQL_FIELD_FLAGS 5
1999-04-21 21:26:10 +00:00
/* {{{ php_mysql_field_info
*/
1999-09-03 19:54:12 +00:00
static void php_mysql_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
1999-04-21 21:26:10 +00:00
{
zval **result, **field;
1999-04-21 21:26:10 +00:00
MYSQL_RES *mysql_result;
MYSQL_FIELD *mysql_field;
char buf[512];
int len;
if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &result, &field)==FAILURE) {
1999-04-21 21:26:10 +00:00
WRONG_PARAM_COUNT;
}
1999-09-03 19:13:37 +00:00
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
1999-04-21 21:26:10 +00:00
convert_to_long_ex(field);
if (Z_LVAL_PP(field)<0 || Z_LVAL_PP(field)>=(int)mysql_num_fields(mysql_result)) {
php_error(E_WARNING, "Field %d is invalid for MySQL result index %d", Z_LVAL_PP(field), Z_LVAL_PP(result));
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
mysql_field_seek(mysql_result, Z_LVAL_PP(field));
1999-04-21 21:26:10 +00:00
if ((mysql_field=mysql_fetch_field(mysql_result))==NULL) {
RETURN_FALSE;
}
switch (entry_type) {
case PHP_MYSQL_FIELD_NAME:
Z_STRLEN_P(return_value) = strlen(mysql_field->name);
Z_STRVAL_P(return_value) = estrndup(mysql_field->name, Z_STRLEN_P(return_value));
Z_TYPE_P(return_value) = IS_STRING;
1999-04-21 21:26:10 +00:00
break;
case PHP_MYSQL_FIELD_TABLE:
Z_STRLEN_P(return_value) = strlen(mysql_field->table);
Z_STRVAL_P(return_value) = estrndup(mysql_field->table, Z_STRLEN_P(return_value));
Z_TYPE_P(return_value) = IS_STRING;
1999-04-21 21:26:10 +00:00
break;
case PHP_MYSQL_FIELD_LEN:
Z_LVAL_P(return_value) = mysql_field->length;
Z_TYPE_P(return_value) = IS_LONG;
1999-04-21 21:26:10 +00:00
break;
case PHP_MYSQL_FIELD_TYPE:
Z_STRVAL_P(return_value) = php_mysql_get_field_name(Z_TYPE_P(mysql_field));
Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
Z_STRVAL_P(return_value) = estrndup(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value));
Z_TYPE_P(return_value) = IS_STRING;
1999-04-21 21:26:10 +00:00
break;
case PHP_MYSQL_FIELD_FLAGS:
1999-04-21 21:26:10 +00:00
strcpy(buf, "");
#ifdef IS_NOT_NULL
if (IS_NOT_NULL(mysql_field->flags)) {
strcat(buf, "not_null ");
}
#endif
#ifdef IS_PRI_KEY
if (IS_PRI_KEY(mysql_field->flags)) {
strcat(buf, "primary_key ");
}
#endif
#ifdef UNIQUE_KEY_FLAG
if (mysql_field->flags&UNIQUE_KEY_FLAG) {
strcat(buf, "unique_key ");
}
#endif
#ifdef MULTIPLE_KEY_FLAG
if (mysql_field->flags&MULTIPLE_KEY_FLAG) {
strcat(buf, "multiple_key ");
}
#endif
#ifdef IS_BLOB
if (IS_BLOB(mysql_field->flags)) {
strcat(buf, "blob ");
}
#endif
#ifdef UNSIGNED_FLAG
if (mysql_field->flags&UNSIGNED_FLAG) {
strcat(buf, "unsigned ");
}
#endif
#ifdef ZEROFILL_FLAG
if (mysql_field->flags&ZEROFILL_FLAG) {
strcat(buf, "zerofill ");
}
#endif
#ifdef BINARY_FLAG
if (mysql_field->flags&BINARY_FLAG) {
strcat(buf, "binary ");
}
#endif
#ifdef ENUM_FLAG
if (mysql_field->flags&ENUM_FLAG) {
strcat(buf, "enum ");
}
#endif
#ifdef SET_FLAG
if (mysql_field->flags&SET_FLAG) {
strcat(buf, "set ");
}
#endif
1999-04-21 21:26:10 +00:00
#ifdef AUTO_INCREMENT_FLAG
if (mysql_field->flags&AUTO_INCREMENT_FLAG) {
strcat(buf, "auto_increment ");
}
#endif
#ifdef TIMESTAMP_FLAG
if (mysql_field->flags&TIMESTAMP_FLAG) {
strcat(buf, "timestamp ");
}
#endif
len = strlen(buf);
/* remove trailing space, if present */
if (len && buf[len-1] == ' ') {
buf[len-1] = 0;
len--;
}
Z_STRLEN_P(return_value) = len;
Z_STRVAL_P(return_value) = estrndup(buf, len);
Z_TYPE_P(return_value) = IS_STRING;
1999-04-21 21:26:10 +00:00
break;
default:
RETURN_FALSE;
}
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ proto string mysql_field_name(int result, int field_index)
2001-11-08 22:05:56 +00:00
Gets the name of the specified field in a result */
PHP_FUNCTION(mysql_field_name)
1999-04-21 21:26:10 +00:00
{
php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_NAME);
1999-04-21 21:26:10 +00:00
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ proto string mysql_field_table(int result, int field_offset)
2001-11-08 22:05:56 +00:00
Gets name of the table the specified field is in */
PHP_FUNCTION(mysql_field_table)
1999-04-21 21:26:10 +00:00
{
php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_TABLE);
1999-04-21 21:26:10 +00:00
}
/* }}} */
2001-02-18 16:59:18 +00:00
/* {{{ proto int mysql_field_len(int result, int field_offset)
1999-04-21 21:26:10 +00:00
Returns the length of the specified field */
PHP_FUNCTION(mysql_field_len)
1999-04-21 21:26:10 +00:00
{
php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_LEN);
1999-04-21 21:26:10 +00:00
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ proto string mysql_field_type(int result, int field_offset)
2001-11-08 22:05:56 +00:00
Gets the type of the specified field in a result */
PHP_FUNCTION(mysql_field_type)
1999-04-21 21:26:10 +00:00
{
php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_TYPE);
1999-04-21 21:26:10 +00:00
}
/* }}} */
1999-04-21 21:26:10 +00:00
/* {{{ proto string mysql_field_flags(int result, int field_offset)
2001-11-08 22:05:56 +00:00
Gets the flags associated with the specified field in a result */
PHP_FUNCTION(mysql_field_flags)
1999-04-21 21:26:10 +00:00
{
php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_FLAGS);
1999-04-21 21:26:10 +00:00
}
/* }}} */
/* {{{ proto bool mysql_free_result(int result)
1999-04-21 21:26:10 +00:00
Free result memory */
PHP_FUNCTION(mysql_free_result)
1999-04-21 21:26:10 +00:00
{
zval **result;
1999-04-21 21:26:10 +00:00
MYSQL_RES *mysql_result;
if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &result)==FAILURE) {
1999-04-21 21:26:10 +00:00
WRONG_PARAM_COUNT;
}
if (Z_TYPE_PP(result)==IS_RESOURCE && Z_LVAL_PP(result)==0) {
1999-04-21 21:26:10 +00:00
RETURN_FALSE;
}
1999-09-03 19:13:37 +00:00
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, result, -1, "MySQL result", le_result);
zend_list_delete(Z_LVAL_PP(result));
1999-04-21 21:26:10 +00:00
RETURN_TRUE;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
1999-04-21 21:26:10 +00:00
*/