Back to index

php5  5.3.10
Defines | Functions
mysqli_nonapi.c File Reference
#include <signal.h>
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_mysqli_structs.h"
#include "mysqli_priv.h"

Go to the source code of this file.

Defines

#define SAFE_STR(a)   ((a)?a:"")
#define zend_parse_parameters_none()   zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "")

Functions

static void php_mysqli_set_error (long mysql_errno, char *mysql_err TSRMLS_DC)
void mysqli_common_connect (INTERNAL_FUNCTION_PARAMETERS, zend_bool is_real_connect, zend_bool in_ctor)
 PHP_FUNCTION (mysqli_connect)
 PHP_FUNCTION (mysqli_link_construct)
 PHP_FUNCTION (mysqli_connect_errno)
 PHP_FUNCTION (mysqli_connect_error)
 PHP_FUNCTION (mysqli_fetch_array)
 PHP_FUNCTION (mysqli_fetch_assoc)
 PHP_FUNCTION (mysqli_fetch_object)
 PHP_FUNCTION (mysqli_multi_query)
 PHP_FUNCTION (mysqli_query)
 PHP_FUNCTION (mysqli_get_warnings)
 PHP_FUNCTION (mysqli_stmt_get_warnings)

Define Documentation

#define SAFE_STR (   a)    ((a)?a:"")

Definition at line 35 of file mysqli_nonapi.c.

Definition at line 38 of file mysqli_nonapi.c.


Function Documentation

void mysqli_common_connect ( INTERNAL_FUNCTION_PARAMETERS  ,
zend_bool  is_real_connect,
zend_bool  in_ctor 
)

Definition at line 59 of file mysqli_nonapi.c.

{
       MY_MYSQL                    *mysql = NULL;
       MYSQLI_RESOURCE             *mysqli_resource = NULL;
       zval                        *object = getThis();
       char                        *hostname = NULL, *username=NULL, *passwd=NULL, *dbname=NULL, *socket=NULL;
       int                                hostname_len = 0, username_len = 0, passwd_len = 0, dbname_len = 0, socket_len = 0;
       zend_bool                   persistent = FALSE;
       long                        port = 0, flags = 0;
       uint                        hash_len;
       char                        *hash_key = NULL;
       zend_bool                   new_connection = FALSE;
       zend_rsrc_list_entry *le;
       mysqli_plist_entry *plist = NULL;
       zend_bool                   self_alloced = 0;


#if !defined(MYSQL_USE_MYSQLND)
       if ((MYSQL_VERSION_ID / 100) != (mysql_get_client_version() / 100)) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING,
                                          "Headers and client library minor version mismatch. Headers:%d Library:%ld",
                                          MYSQL_VERSION_ID, mysql_get_client_version());
       }
#endif

       if (getThis() && !ZEND_NUM_ARGS() && in_ctor) {
              php_mysqli_init(INTERNAL_FUNCTION_PARAM_PASSTHRU);
              return;
       }
       hostname = username = dbname = passwd = socket = NULL;

       if (!is_real_connect) {
              if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ssssls", &hostname, &hostname_len, &username, &username_len,
                                                               &passwd, &passwd_len, &dbname, &dbname_len, &port, &socket, &socket_len) == FAILURE) {
                     return;
              }

              if (object && instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry TSRMLS_CC)) {
                     mysqli_resource = ((mysqli_object *) zend_object_store_get_object(object TSRMLS_CC))->ptr;
                     if (mysqli_resource && mysqli_resource->ptr) {
                            mysql = (MY_MYSQL*) mysqli_resource->ptr;
                     }
              }
              if (!mysql) {
                     mysql = (MY_MYSQL *) ecalloc(1, sizeof(MY_MYSQL));
                     self_alloced = 1;
              }
              flags |= CLIENT_MULTI_RESULTS; /* needed for mysql_multi_query() */
       } else {
              /* We have flags too */
              if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|sssslsl", &object, mysqli_link_class_entry,
                                                                      &hostname, &hostname_len, &username, &username_len, &passwd, &passwd_len, &dbname, &dbname_len, &port, &socket, &socket_len,
                                                                      &flags) == FAILURE) {
                     return;
              }

              mysqli_resource = ((mysqli_object *) zend_object_store_get_object(object TSRMLS_CC))->ptr;
              MYSQLI_FETCH_RESOURCE_CONN(mysql, &object, MYSQLI_STATUS_INITIALIZED);

              /* set some required options */
              flags |= CLIENT_MULTI_RESULTS; /* needed for mysql_multi_query() */
              /* remove some insecure options */
              flags &= ~CLIENT_MULTI_STATEMENTS;   /* don't allow multi_queries via connect parameter */
              if (PG(open_basedir) && PG(open_basedir)[0] != '\0') {
                     flags &= ~CLIENT_LOCAL_FILES;
              }
       }

       if (!socket_len || !socket) {
              socket = MyG(default_socket);
       }
       if (!port){
              port = MyG(default_port);
       }
       if (!passwd) {
              passwd = MyG(default_pw);
              passwd_len = strlen(SAFE_STR(passwd));
       }
       if (!username){
              username = MyG(default_user);
       }
       if (!hostname || !hostname_len) {
              hostname = MyG(default_host);
       }

       if (mysql->mysql && mysqli_resource &&
              (mysqli_resource->status > MYSQLI_STATUS_INITIALIZED))
       {
              /* already connected, we should close the connection */
              php_mysqli_close(mysql, MYSQLI_CLOSE_IMPLICIT, mysqli_resource->status TSRMLS_CC);
       }

       if (strlen(SAFE_STR(hostname)) > 2 && !strncasecmp(hostname, "p:", 2)) {
              hostname += 2;
              if (!MyG(allow_persistent)) {
                     php_error_docref(NULL TSRMLS_CC, E_WARNING, "Persistent connections are disabled. Downgrading to normal");
              } else {
                     mysql->persistent = persistent = TRUE;

                     hash_len = spprintf(&hash_key, 0, "mysqli_%s_%s%ld%s%s%s", SAFE_STR(hostname), SAFE_STR(socket),
                                                        port, SAFE_STR(username), SAFE_STR(dbname),
                                                        SAFE_STR(passwd));

                     mysql->hash_key = hash_key;

                     /* check if we can reuse exisiting connection ... */
                     if (zend_hash_find(&EG(persistent_list), hash_key, hash_len + 1, (void **)&le) == SUCCESS) {
                            if (Z_TYPE_P(le) == php_le_pmysqli()) {
                                   plist = (mysqli_plist_entry *) le->ptr;

                                   do {
                                          if (zend_ptr_stack_num_elements(&plist->free_links)) {
                                                 mysql->mysql = zend_ptr_stack_pop(&plist->free_links);

                                                 MyG(num_inactive_persistent)--;
                                                 /* reset variables */

#ifndef MYSQLI_NO_CHANGE_USER_ON_PCONNECT
                                                 if (!mysqli_change_user_silent(mysql->mysql, username, passwd, dbname)) {
#else
                                                 if (!mysql_ping(mysql->mysql)) {
#endif
#ifdef MYSQLI_USE_MYSQLND
                                                        mysqlnd_restart_psession(mysql->mysql);
#endif
                                                        MyG(num_active_persistent)++;
                                                        goto end;
                                                 } else {
                                                        mysqli_close(mysql->mysql, MYSQLI_CLOSE_IMPLICIT);
                                                        mysql->mysql = NULL;
                                                 }
                                          }
                                   } while (0);
                            }
                     } else {
                            zend_rsrc_list_entry le;
                            le.type = php_le_pmysqli();
                            le.ptr = plist = calloc(1, sizeof(mysqli_plist_entry));

                            zend_ptr_stack_init_ex(&plist->free_links, 1);
                            zend_hash_update(&EG(persistent_list), hash_key, hash_len + 1, (void *)&le, sizeof(le), NULL);
                     }
              }
       }
       if (MyG(max_links) != -1 && MyG(num_links) >= MyG(max_links)) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open links (%ld)", MyG(num_links));
              goto err;
       }

       if (persistent && MyG(max_persistent) != -1 &&
              (MyG(num_active_persistent) + MyG(num_inactive_persistent))>= MyG(max_persistent))
       {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open persistent links (%ld)",
                                                        MyG(num_active_persistent) + MyG(num_inactive_persistent));
              goto err;
       }
       if (!mysql->mysql) {
#if !defined(MYSQLI_USE_MYSQLND)
              if (!(mysql->mysql = mysql_init(NULL))) {
#else
              if (!(mysql->mysql = mysqlnd_init(persistent))) {
#endif
                     goto err;
              }
              new_connection = TRUE;
       }

#ifdef HAVE_EMBEDDED_MYSQLI
       if (hostname_len) {
              unsigned int external=1;
              mysql_options(mysql->mysql, MYSQL_OPT_USE_REMOTE_CONNECTION, (char *)&external);
       } else {
              mysql_options(mysql->mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, 0);
       }
#endif

#if !defined(MYSQLI_USE_MYSQLND)
       /* BC for prior to bug fix #53425 */
       flags |= CLIENT_MULTI_RESULTS;

       if (mysql_real_connect(mysql->mysql, hostname, username, passwd, dbname, port, socket, flags) == NULL)
#else
       if (mysqlnd_connect(mysql->mysql, hostname, username, passwd, passwd_len, dbname, dbname_len,
                                          port, socket, flags TSRMLS_CC) == NULL)
#endif
       {
              /* Save error messages - for mysqli_connect_error() & mysqli_connect_errno() */
              php_mysqli_set_error(mysql_errno(mysql->mysql), (char *) mysql_error(mysql->mysql) TSRMLS_CC);
              php_mysqli_throw_sql_exception((char *)mysql_sqlstate(mysql->mysql), mysql_errno(mysql->mysql) TSRMLS_CC,
                                                                      "%s", mysql_error(mysql->mysql));
              if (!is_real_connect) {
                     /* free mysql structure */
                     mysqli_close(mysql->mysql, MYSQLI_CLOSE_DISCONNECTED);
                     mysql->mysql = NULL;
              }
              goto err;
       }

       /* clear error */
       php_mysqli_set_error(mysql_errno(mysql->mysql), (char *) mysql_error(mysql->mysql) TSRMLS_CC);

#if !defined(MYSQLI_USE_MYSQLND)
       mysql->mysql->reconnect = MyG(reconnect);

       /* set our own local_infile handler */
       php_set_local_infile_handler_default(mysql);
#endif

       mysql_options(mysql->mysql, MYSQL_OPT_LOCAL_INFILE, (char *)&MyG(allow_local_infile));

end:
       if (!mysqli_resource) {
              mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
              mysqli_resource->ptr = (void *)mysql;
       }
       mysqli_resource->status = MYSQLI_STATUS_VALID;

       /* store persistent connection */
       if (persistent && (new_connection || is_real_connect)) {
              MyG(num_active_persistent)++;
       }

       MyG(num_links)++;

       mysql->multi_query = 0;

       if (!object || !instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry TSRMLS_CC)) {
              MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_link_class_entry);
       } else {
              ((mysqli_object *) zend_object_store_get_object(object TSRMLS_CC))->ptr = mysqli_resource;
       }
       if (!is_real_connect) {
              return;
       } else {
              RETURN_TRUE;
       }

err:
       if (mysql->hash_key) {
              efree(mysql->hash_key);
              mysql->hash_key = NULL;
              mysql->persistent = FALSE;
       }
       if (!is_real_connect && self_alloced) {
              efree(mysql);
       }
       RETVAL_FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PHP_FUNCTION ( mysqli_connect  )

Definition at line 311 of file mysqli_nonapi.c.

Here is the call graph for this function:

PHP_FUNCTION ( mysqli_link_construct  )

Definition at line 320 of file mysqli_nonapi.c.

Here is the call graph for this function:

PHP_FUNCTION ( mysqli_connect_errno  )

Definition at line 329 of file mysqli_nonapi.c.

PHP_FUNCTION ( mysqli_connect_error  )

Definition at line 337 of file mysqli_nonapi.c.

{
       if (MyG(error_msg)) {
              RETURN_STRING(MyG(error_msg),1);
       } else {
              RETURN_NULL();
       }
}
PHP_FUNCTION ( mysqli_fetch_array  )

Definition at line 350 of file mysqli_nonapi.c.

Here is the call graph for this function:

PHP_FUNCTION ( mysqli_fetch_assoc  )

Definition at line 358 of file mysqli_nonapi.c.

Here is the call graph for this function:

PHP_FUNCTION ( mysqli_fetch_object  )

Definition at line 435 of file mysqli_nonapi.c.

Here is the call graph for this function:

PHP_FUNCTION ( mysqli_multi_query  )

Definition at line 443 of file mysqli_nonapi.c.

{
       MY_MYSQL      *mysql;
       zval          *mysql_link;
       char          *query = NULL;
       int           query_len;

       if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &mysql_link, mysqli_link_class_entry, &query, &query_len) == FAILURE) {
              return;
       }
       MYSQLI_FETCH_RESOURCE_CONN(mysql, &mysql_link, MYSQLI_STATUS_VALID);

       MYSQLI_ENABLE_MQ;
       if (mysql_real_query(mysql->mysql, query, query_len)) {
#ifndef MYSQLI_USE_MYSQLND
              char s_error[MYSQL_ERRMSG_SIZE], s_sqlstate[SQLSTATE_LENGTH+1];
              unsigned int s_errno;
              /* we have to save error information, cause
              MYSQLI_DISABLE_MQ will reset error information */
              strcpy(s_error, mysql_error(mysql->mysql));
              strcpy(s_sqlstate, mysql_sqlstate(mysql->mysql));
              s_errno = mysql_errno(mysql->mysql);
#else
              MYSQLND_ERROR_INFO error_info = mysql->mysql->error_info;
#endif
              MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
              MYSQLI_DISABLE_MQ;

#ifndef MYSQLI_USE_MYSQLND
              /* restore error information */
              strcpy(mysql->mysql->net.last_error, s_error);
              strcpy(mysql->mysql->net.sqlstate, s_sqlstate);
              mysql->mysql->net.last_errno = s_errno;
#else
              mysql->mysql->error_info = error_info;
#endif
              RETURN_FALSE;
       }
       RETURN_TRUE;
}

Here is the call graph for this function:

PHP_FUNCTION ( mysqli_query  )

Definition at line 486 of file mysqli_nonapi.c.

{
       MY_MYSQL                    *mysql;
       zval                        *mysql_link;
       MYSQLI_RESOURCE             *mysqli_resource;
       MYSQL_RES                   *result;
       char                        *query = NULL;
       int                         query_len;
       long                        resultmode = MYSQLI_STORE_RESULT;

       if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|l", &mysql_link, mysqli_link_class_entry, &query, &query_len, &resultmode) == FAILURE) {
              return;
       }

       if (!query_len) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty query");
              RETURN_FALSE;
       }
       if ((resultmode & ~MYSQLI_ASYNC) != MYSQLI_USE_RESULT && (resultmode & ~MYSQLI_ASYNC) != MYSQLI_STORE_RESULT) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid value for resultmode");
              RETURN_FALSE;
       }

       MYSQLI_FETCH_RESOURCE_CONN(mysql, &mysql_link, MYSQLI_STATUS_VALID);

       MYSQLI_DISABLE_MQ;


#ifdef MYSQLI_USE_MYSQLND
       if (resultmode & MYSQLI_ASYNC) {
              if (mysqli_async_query(mysql->mysql, query, query_len)) {
                     MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
                     RETURN_FALSE;
              }
              mysql->async_result_fetch_type = resultmode & ~MYSQLI_ASYNC;
              RETURN_TRUE;
       }
#endif

       if (mysql_real_query(mysql->mysql, query, query_len)) {
              MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
              RETURN_FALSE;
       }

       if (!mysql_field_count(mysql->mysql)) {
              /* no result set - not a SELECT */
              if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
                     php_mysqli_report_index(query, mysqli_server_status(mysql->mysql) TSRMLS_CC);
              }
              RETURN_TRUE;
       }

       switch (resultmode) {
              case MYSQLI_STORE_RESULT:
                     result = mysql_store_result(mysql->mysql);
                     break;
              case MYSQLI_USE_RESULT:
                     result = mysql_use_result(mysql->mysql);
                     break;
       }
       if (!result) {
              php_mysqli_throw_sql_exception((char *)mysql_sqlstate(mysql->mysql), mysql_errno(mysql->mysql) TSRMLS_CC,
                                                                      "%s", mysql_error(mysql->mysql));
              RETURN_FALSE;
       }

       if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
              php_mysqli_report_index(query, mysqli_server_status(mysql->mysql) TSRMLS_CC);
       }

       mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
       mysqli_resource->ptr = (void *)result;
       mysqli_resource->status = MYSQLI_STATUS_VALID;
       MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_result_class_entry);
}

Here is the call graph for this function:

PHP_FUNCTION ( mysqli_get_warnings  )

Definition at line 827 of file mysqli_nonapi.c.

{
       MY_MYSQL                    *mysql;
       zval                        *mysql_link;
       MYSQLI_RESOURCE             *mysqli_resource;
       MYSQLI_WARNING              *w;

       if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
              return;
       }
       MYSQLI_FETCH_RESOURCE_CONN(mysql, &mysql_link, MYSQLI_STATUS_VALID);

       if (mysql_warning_count(mysql->mysql)) {
              w = php_get_warnings(mysql->mysql TSRMLS_CC);
       } else {
              RETURN_FALSE;
       }
       mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
       mysqli_resource->ptr = mysqli_resource->info = (void *)w;
       mysqli_resource->status = MYSQLI_STATUS_VALID;
       MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_warning_class_entry);
}

Here is the call graph for this function:

PHP_FUNCTION ( mysqli_stmt_get_warnings  )

Definition at line 852 of file mysqli_nonapi.c.

{
       MY_STMT                            *stmt;
       zval                        *stmt_link;
       MYSQLI_RESOURCE             *mysqli_resource;
       MYSQLI_WARNING              *w;

       if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &stmt_link, mysqli_stmt_class_entry) == FAILURE) {
              return;
       }
       MYSQLI_FETCH_RESOURCE_STMT(stmt, &stmt_link, MYSQLI_STATUS_VALID);

       if (mysqli_stmt_warning_count(stmt->stmt)) {
              w = php_get_warnings(mysqli_stmt_get_connection(stmt->stmt) TSRMLS_CC);
       } else {
              RETURN_FALSE;
       }
       mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
       mysqli_resource->ptr = mysqli_resource->info = (void *)w;
       mysqli_resource->status = MYSQLI_STATUS_VALID;
       MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_warning_class_entry);
}

Here is the call graph for this function:

static void php_mysqli_set_error ( long  mysql_errno,
char *mysql_err  TSRMLS_DC 
) [static]

Definition at line 44 of file mysqli_nonapi.c.

{
       MyG(error_no) = mysql_errno;
       if (MyG(error_msg)) {
              efree(MyG(error_msg));
       }
       if(mysql_err && *mysql_err) {
              MyG(error_msg) = estrdup(mysql_err);
       } else {
              MyG(error_msg) = NULL;
       }
}

Here is the caller graph for this function: