diff --git a/mysql.php b/mysql.php index 0ef2ebc..94884f9 100644 --- a/mysql.php +++ b/mysql.php @@ -14,11 +14,6 @@ trigger_error('The extension "MySQLi" is not available', E_USER_ERROR); } - // The function name "getLinkIdentifier" will be used to return a valid link_indentifier, make it is available - if (function_exists('getLinkIdentifier')) { - trigger_error('The function name "getLinkIdentifier" is already defined, please change the function name', E_USER_ERROR); - } - // Define MySQL constants define('MYSQL_CLIENT_COMPRESS', MYSQLI_CLIENT_COMPRESS); define('MYSQL_CLIENT_IGNORE_SPACE', MYSQLI_CLIENT_IGNORE_SPACE); @@ -29,23 +24,43 @@ define('MYSQL_NUM', MYSQLI_NUM); define('MYSQL_BOTH', MYSQLI_BOTH); + // Will contain the link identifier - $link = null; - - /** - * Get the link identifier - * - * @param mysqli $mysqli - * @return mysqli|null - */ - function getLinkIdentifier(mysqli $mysqli = null) - { - if (!$mysqli) { - global $link; - $mysqli = $link; + class mysql_global { + static $link = null; + + /** + * Get the link identifier + * + * @param mysqli $mysqli + * @return mysqli|null + */ + static function getLink( mysqli $mysqli = null ){ + if (!$mysqli) { + $mysqli = self::$link; + } + return $mysqli; + } + } + + function mysql_valid_result($result){ + if( $result instanceof mysqli_result ){ + return true; + }else{ + $type = gettype( $result ); + $trace = debug_backtrace(); + $c = $trace[1]; + $message = sprintf("PHP Warning: %s expected mysqli_result, %s given - %s on line %s", + $c['function'], $type, $c['file'], $c['line'] + ); + // This is to show where the error is coming from. + error_log($message); + // This is for the stack trace and where this message is coming from. + + $message = "Invalid mysqli_result!"; + trigger_error($message, E_USER_WARNING ); + return false; } - - return $mysqli; } /** @@ -58,10 +73,8 @@ function getLinkIdentifier(mysqli $mysqli = null) */ function mysql_connect($server, $username, $password, $new_link = false, $client_flags = 0) { - global $link; - - $link = mysqli_connect($server, $username, $password); - return $link; + mysql_global::$link = mysqli_connect($server, $username, $password); + return mysql_global::$link; } /** @@ -74,21 +87,17 @@ function mysql_connect($server, $username, $password, $new_link = false, $client */ function mysql_pconnect($server, $username, $password, $new_link = false, $client_flags = 0) { - global $link; - - $link = mysqli_connect('p:' . $server, $username, $password); - return $link; + mysql_global::$link = mysqli_connect('p:' . $server, $username, $password); + return mysql_global::$link; } /** * @param $databaseName * @return bool */ - function mysql_select_db($databaseName) + function mysql_select_db($databaseName, mysqli $mysqli = null ) { - global $link; - - return mysqli_select_db($link, $databaseName); + return mysqli_select_db(mysql_global::getLink($mysqli), $databaseName); } /** @@ -98,7 +107,7 @@ function mysql_select_db($databaseName) */ function mysql_query($query, mysqli $mysqli = null) { - return getLinkIdentifier($mysqli)->query($query); + return mysql_global::getLink($mysqli)->query($query); } /** @@ -108,63 +117,43 @@ function mysql_query($query, mysqli $mysqli = null) */ function mysql_real_escape_string($string, mysqli $mysqli = null) { - return getLinkIdentifier($mysqli)->escape_string($string); + return mysql_global::getLink($mysqli)->escape_string($string); } /** * @param mysqli_result $result * @return bool|array */ - function mysql_fetch_assoc(mysqli_result $result) + function mysql_fetch_assoc($result) { - $result = $result->fetch_assoc(); - if ($result === NULL) { - $result = false; - } - - return $result; + return mysql_valid_result( $result ) ? $result->fetch_assoc() : false; } /** * @param mysqli_result $result * @return object|stdClass */ - function mysql_fetch_object(mysqli_result $result) + function mysql_fetch_object( $result ) { - $result = $result->fetch_object(); - if ($result === NULL) { - $result = false; - } - - return $result; + return mysql_valid_result( $result ) ? $result->fetch_object() : false; } /** * @param mysqli_result $result * @return bool|int */ - function mysql_num_rows(mysqli_result $result) + function mysql_num_rows( $result ) { - $result = $result->num_rows; - if ($result === NULL) { - $result = false; - } - - return $result; + return mysql_valid_result( $result ) ? $result->num_rows : false; } /** * @param mysqli_result $result * @return bool|array */ - function mysql_fetch_row(mysqli_result $result) + function mysql_fetch_row( $result ) { - $result = $result->fetch_row(); - if ($result === NULL) { - $result = false; - } - - return $result; + return mysql_valid_result( $result ) ? $result->fetch_row() : false; } /** @@ -173,7 +162,7 @@ function mysql_fetch_row(mysqli_result $result) */ function mysql_affected_rows(mysqli $mysqli = null) { - return mysqli_affected_rows(getLinkIdentifier($mysqli)); + return mysqli_affected_rows(mysql_global::getLink($mysqli)); } /** @@ -181,7 +170,7 @@ function mysql_affected_rows(mysqli $mysqli = null) */ function mysql_client_encoding(mysqli $mysqli = null) { - return mysqli_character_set_name(getLinkIdentifier($mysqli)); + return mysqli_character_set_name(mysql_global::getLink($mysqli)); } /** @@ -190,7 +179,7 @@ function mysql_client_encoding(mysqli $mysqli = null) */ function mysql_close(mysqli $mysqli = null) { - return mysqli_close(getLinkIdentifier($mysqli)); + return mysqli_close(mysql_global::getLink($mysqli)); } /** @@ -208,20 +197,26 @@ function mysql_create_db($database_name, mysqli $mysqli = null) */ function mysql_errno(mysqli $mysqli = null) { - return mysqli_errno(getLinkIdentifier($mysqli)); + return mysqli_errno(mysql_global::getLink($mysqli)); } /** - * Not implemented - * - * @todo implement + * Adjusts the result pointer to an arbitrary row in the result * - * @return null + * @param $result + * @param $row + * @param int $field + * @return bool */ - function mysql_db_name() + function mysql_db_name(mysqli_result $result, $row, $field=null) { - trigger_error('The function mysql_db_name() is not implemented', E_USER_WARNING); - return false; + if( mysql_valid_result( $result ) ){ + $result->data_seek( $row ); + $f = $result->fetch_row(); + return $f[0]; + }else{ + return false; + } } /** @@ -230,7 +225,7 @@ function mysql_db_name() */ function mysql_error(mysqli $mysqli = null) { - return mysqli_error(getLinkIdentifier($mysqli)); + return mysqli_error(mysql_global::getLink($mysqli)); } /** @@ -238,9 +233,9 @@ function mysql_error(mysqli $mysqli = null) * @param $result_type * @return void */ - function mysql_fetch_array(mysqli_result $result, $result_type = MYSQL_BOTH) + function mysql_fetch_array($result, $result_type = MYSQL_BOTH) { - return mysqli_fetch_array($result, $result_type); + return mysql_valid_result( $result ) ? $result->fetch_array($result_type) : false; } /** @@ -249,7 +244,7 @@ function mysql_fetch_array(mysqli_result $result, $result_type = MYSQL_BOTH) */ function mysql_ping(mysqli $mysqli = null) { - return mysqli_ping(getLinkIdentifier($mysqli)); + return mysqli_ping(mysql_global::getLink($mysqli)); } /** @@ -258,7 +253,7 @@ function mysql_ping(mysqli $mysqli = null) */ function mysql_unbuffered_query($query, mysqli $mysqli = null) { - return mysqli_query(getLinkIdentifier($mysqli), $query, MYSQLI_USE_RESULT); + return mysqli_query(mysql_global::getLink($mysqli), $query, MYSQLI_USE_RESULT); } /** @@ -273,9 +268,9 @@ function mysql_get_client_info() * @param mysqli_result $result * @return void */ - function mysql_free_result(mysqli_result $result) + function mysql_free_result($result) { - return mysqli_free_result($result); + return mysql_valid_result( $result ) ? $result->free() : false; } /** @@ -286,7 +281,7 @@ function mysql_list_dbs(mysqli $mysqli = null) { trigger_error('This function is deprecated. It is preferable to use mysql_query() to issue an SQL Query: SHOW DATABASES statement instead.', E_USER_DEPRECATED); - return mysqli_query(getLinkIdentifier($mysqli), 'SHOW DATABASES'); + return mysqli_query(mysql_global::getLink($mysqli), 'SHOW DATABASES'); } /** @@ -299,7 +294,7 @@ function mysql_list_fields($database_name, $table_name, mysqli $mysqli = null) { trigger_error('This function is deprecated. It is preferable to use mysql_query() to issue an SQL SHOW COLUMNS FROM table [LIKE \'name\'] statement instead.', E_USER_DEPRECATED); - $mysqli = getLinkIdentifier($mysqli); + $mysqli = mysql_global::getLink($mysqli); $db = mysqli_escape_string($mysqli, $database_name); $table = mysqli_escape_string($mysqli, $table_name); @@ -312,7 +307,7 @@ function mysql_list_fields($database_name, $table_name, mysqli $mysqli = null) */ function mysql_list_processes(mysqli $mysqli = null) { - return mysqli_query(getLinkIdentifier($mysqli), 'SHOW PROCESSLIST'); + return mysqli_query(mysql_global::getLink($mysqli), 'SHOW PROCESSLIST'); } /** @@ -322,7 +317,7 @@ function mysql_list_processes(mysqli $mysqli = null) */ function mysql_set_charset($charset, mysqli $mysqli = null) { - return mysqli_set_charset(getLinkIdentifier($mysqli), $charset); + return mysqli_set_charset(mysql_global::getLink($mysqli), $charset); } /** @@ -331,7 +326,7 @@ function mysql_set_charset($charset, mysqli $mysqli = null) */ function mysql_info(mysqli $mysqli = null) { - $result = mysqli_info(getLinkIdentifier($mysqli)); + $result = mysqli_info(mysql_global::getLink($mysqli)); if ($result === NULL) { $result = false; } @@ -347,7 +342,7 @@ function mysql_info(mysqli $mysqli = null) */ function mysql_stat(mysqli $mysqli = null) { - return mysqli_stat(getLinkIdentifier($mysqli)); + return mysqli_stat(mysql_global::getLink($mysqli)); } /** @@ -358,7 +353,7 @@ function mysql_stat(mysqli $mysqli = null) */ function mysql_thread_id(mysqli $mysqli = null) { - return mysqli_thread_id(getLinkIdentifier($mysqli)); + return mysqli_thread_id(mysql_global::getLink($mysqli)); } /** @@ -369,7 +364,7 @@ function mysql_thread_id(mysqli $mysqli = null) */ function mysql_get_host_info(mysqli $mysqli = null) { - return mysqli_get_host_info(getLinkIdentifier($mysqli)); + return mysqli_get_host_info(mysql_global::getLink($mysqli)); } /** @@ -380,7 +375,7 @@ function mysql_get_host_info(mysqli $mysqli = null) */ function mysql_get_proto_info(mysqli $mysqli = null) { - return mysqli_get_proto_info(getLinkIdentifier($mysqli)); + return mysqli_get_proto_info(mysql_global::getLink($mysqli)); } /** @@ -391,20 +386,25 @@ function mysql_get_proto_info(mysqli $mysqli = null) */ function mysql_get_server_info(mysqli $mysqli = null) { - return mysqli_get_server_info(getLinkIdentifier($mysqli)); + return mysqli_get_server_info(mysql_global::getLink($mysqli)); } /** * Get table name of field * * @param $result - * @param $i + * @param $row * @return bool */ - function mysql_tablename($result, $i) + function mysql_tablename($result, $row) { - trigger_error('Not implemented', E_USER_WARNING); - return false; + if( !mysql_valid_result($result)){ + return false; + } + + $result->data_seek($row); + $f = $result->fetch_array(); + return $f[0]; } /** @@ -415,7 +415,7 @@ function mysql_tablename($result, $i) */ function mysql_insert_id(mysqli $mysqli = null) { - return mysqli_insert_id(getLinkIdentifier($mysqli)); + return mysqli_insert_id(mysql_global::getLink($mysqli)); } /** @@ -443,9 +443,9 @@ function mysql_result($result, $row, $field = 0) * @param mysqli_result $result * @return int */ - function mysql_num_fields(mysqli_result $result) + function mysql_num_fields($result) { - return mysqli_num_fields($result); + return mysql_valid_result($result) ? $result->num_fields() : false; } /** @@ -458,7 +458,7 @@ function mysql_list_tables($database_name, mysqli $mysqli = null) { trigger_error('This function is deprecated. It is preferable to use mysql_query() to issue an SQL SHOW TABLES [FROM db_name] [LIKE \'pattern\'] statement instead.', E_USER_DEPRECATED); - $mysqli = getLinkIdentifier($mysqli); + $mysqli = mysql_global::getLink($mysqli); $db = mysqli_escape_string($mysqli, $database_name); return mysqli_query($mysqli, sprintf('SHOW TABLES FROM %s', $db)); @@ -471,13 +471,15 @@ function mysql_list_tables($database_name, mysqli $mysqli = null) * @param int $field_offset * @return bool|object */ - function mysql_fetch_field(mysqli_result $result, $field_offset = 0) + function mysql_fetch_field($result, $field_offset = 0) { - if ($field_offset) { - mysqli_field_seek($result, $field_offset); + if(!mysql_valid_result($result)){ + return false; } - - return mysqli_fetch_field($result); + + $field_offset = (int)$field_offset; + $result->field_seek($field_offset); + return $result->fetch_field(); } /** @@ -487,10 +489,14 @@ function mysql_fetch_field(mysqli_result $result, $field_offset = 0) * @param int $field_offset * @return bool */ - function mysql_field_len(mysqli_result $result, $field_offset = 0) + function mysql_field_len($result, $field_offset = 0) { - trigger_error('This function is not implemented', E_USER_WARNING); - return false; + if(!mysql_valid_result($result)){ + return false; + } + + $fieldInfo = $result->fetch_field_direct( $field_offset ); + return $fieldInfo->length; } /** @@ -509,9 +515,9 @@ function mysql_drop_db() * @param int $row_number * @return void */ - function mysql_data_seek(mysqli_result $result, $row_number = 0) + function mysql_data_seek( $result, $row_number = 0) { - return mysqli_data_seek($result, $row_number); + return mysql_valid_result($result) ? $result->data_seek($row_number) : false; } /** @@ -533,9 +539,9 @@ function mysql_field_name($result, $field_offset = 0) * @param mysqli_result $result * @return array|bool */ - function mysql_fetch_lengths(mysqli_result $result) + function mysql_fetch_lengths($result) { - return mysqli_fetch_lengths($result); + return mysql_valid_result($result) ? $result->fetch_lengths() : false; } /** @@ -544,10 +550,14 @@ function mysql_fetch_lengths(mysqli_result $result) * @param $field_offset * @return string */ - function mysql_field_type(mysqli_result $result, $field_offset = 0) + function mysql_field_type($result, $field_offset = 0) { + if(!mysql_valid_result($result)){ + return false; + } + $unknown = 'unknown'; - $info = mysqli_fetch_field_direct($result, $field_offset); + $info = $result->fetch_field_direct($field_offset); if (empty($info->type)) { return $unknown; } @@ -621,30 +631,48 @@ function mysql_field_type(mysqli_result $result, $field_offset = 0) * @param $field_offset * @return bool */ - function mysql_field_table(mysqli_result $result, $field_offset = 0) + function mysql_field_table($result, $field_offset = 0) { - $info = mysqli_fetch_field_direct($result, $field_offset); - if (empty($info->table)) { + if(!mysql_valid_result($result)){ return false; } - - return $info->table; + $info = $result->fetch_field_direct($field_offset); + return !(empty($info->table)) ? $info->table : false; } /** * Get the flags associated with the specified field in a result * - * @todo implement + * credit to Dave Smith from phpclasses.org, andre at koethur dot de from php.net and NinjaKC from stackoverflow.com * * @param mysqli_result $result * @param int $field_offset * @return bool */ - function mysql_field_flags(mysqli_result $result, $field_offset = 0) + function mysql_field_flags( $result, $field_offset = 0) { - trigger_error('This function is not implemented', E_USER_WARNING); - return false; - } + if(!mysql_valid_result($result)){ + return false; + } + + $flags_num = $result->fetch_field_direct($field_offset)->flags; + + if (!isset($flags)) + { + $flags = array(); + $constants = get_defined_constants(true); + foreach ($constants['mysqli'] as $c => $n) if (preg_match('/MYSQLI_(.*)_FLAG$/', $c, $m)) if (!array_key_exists($n, $flags)) $flags[$n] = $m[1]; + } + + $result = array(); + foreach ($flags as $n => $t) if ($flags_num & $n) $result[] = $t; + + $return = implode(' ', $result); + $return = str_replace('PRI_KEY','PRIMARY_KEY',$return); + $return = strtolower($return); + + return $return; + } /** * Set result pointer to a specified field offset @@ -653,9 +681,9 @@ function mysql_field_flags(mysqli_result $result, $field_offset = 0) * @param int $field_offset * @return bool */ - function mysql_field_seek(mysqli_result $result, $field_offset = 0) + function mysql_field_seek($result, $field_offset = 0) { - return mysqli_field_seek($result, $field_offset); + return mysql_valid_result($result) ? $result->field_seek($field_offset) : false; } /**