Files
phpmyadmin/libraries/dbi/mysqli.dbi.lib.php
Alexander M. Turek 6279474b3a Charset stuff
2004-02-15 01:07:20 +00:00

227 lines
7.0 KiB
PHP

<?php
/* $Id$ */
// vim: expandtab sw=4 ts=4 sts=4:
/**
* Interface to the improved MySQL extension (MySQLi)
*/
/**
* Loads the MySQLi extension if it is not loaded yet
*/
if (!@function_exists('mysqli_connect')) {
PMA_dl('mysqli');
}
// check whether mysql is available
if (!@function_exists('mysqli_connect')) {
require_once('./libraries/header_http.inc.php');
echo sprintf($strCantLoad, 'mysqli') . '<br />' . "\n"
. '<a href="./Documentation.html#faqmysql" target="documentation">' . $GLOBALS['strDocu'] . '</a>' . "\n";
exit;
}
// MySQL client API
if (!defined('PMA_MYSQL_CLIENT_API')) {
$client_api = explode('.', mysqli_get_client_info());
define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2])));
unset($client_api);
}
function PMA_DBI_connect($user, $password) {
global $cfg, $php_errormsg;
$server_port = (empty($cfg['Server']['port']))
? FALSE
: (int) $cfg['Server']['port'];
if (strtolower($cfg['Server']['connect_type']) == 'tcp') {
$cfg['Server']['socket'] = '';
}
$server_socket = (empty($cfg['Server']['socket']))
? FALSE
: $cfg['Server']['socket'];
$link = @mysqli_connect($cfg['Server']['host'], $user, $password, FALSE, $server_port, $server_socket);
if (empty($link)) {
PMA_auth_fails();
} // end if
if (!defined('PMA_MYSQL_INT_VERSION')) {
$result = mysqli_query($link, 'SELECT VERSION() AS version;', MYSQLI_STORE_RESULT);
if ($result != FALSE && @mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_row($result);
$match = explode('.', $row[0]);
mysqli_free_result($result);
}
if (!isset($row)) {
define('PMA_MYSQL_INT_VERSION', 32332);
define('PMA_MYSQL_STR_VERSION', '3.23.32');
} else{
define('PMA_MYSQL_INT_VERSION', (int)sprintf('%d%02d%02d', $match[0], $match[1], intval($match[2])));
define('PMA_MYSQL_STR_VERSION', $row[0]);
unset($result, $row, $match);
}
}
if (PMA_MYSQL_INT_VERSION >= 40100) {
$mysql_charset = $GLOBALS['mysql_charset_map'][$GLOBALS['charset']];
mysqli_query($link, 'SET CHARACTER SET ' . $mysql_charset . ';', MYSQLI_STORE_RESULT);
mysqli_query($link, 'SET SESSION character_set_connection = \'' . $mysql_charset . '\';', MYSQLI_STORE_RESULT);
} else {
require_once('./libraries/charset_conversion.lib.php');
}
return $link;
}
function PMA_DBI_select_db($dbname, $link = NULL) {
if (empty($link)) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return FALSE;
}
}
if (PMA_MYSQL_INT_VERSION < 40100) {
$dbname = PMA_convert_charset($dbname);
}
return mysqli_select_db($link, $dbname);
}
function PMA_DBI_try_query($query, $link = NULL, $options = 0) {
if ($options == $options | PMA_DBI_QUERY_STORE) {
$method = MYSQLI_STORE_RESULT;
} else {
$method = MYSQLI_USE_RESULT;
}
if (empty($link)) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return FALSE;
}
}
if (PMA_MYSQL_INT_VERSION < 40100) {
$query = PMA_convert_charset($query);
}
return mysqli_query($link, $query, $method);
}
// The following function is meant for internal use only.
// Do not call it from outside this library!
function PMA_mysqli_fetch_array($result, $type = FALSE) {
global $cfg, $allow_recoding, $charset, $convcharset;
if ($type != FALSE) {
$data = mysqli_fetch_array($result, $type);
} else {
$data = mysqli_fetch_array($result);
}
if (PMA_MYSQL_INT_VERSION >= 40100
|| !(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
/* No recoding -> return data as we got them */
return $data;
} else {
$ret = array();
$num = mysqli_num_fields($result);
$fields = mysqli_fetch_fields($result);
$i = 0;
for($i = 0; $i < $num; $i++) {
if (!$meta) {
/* No meta information available -> we guess that it should be converted */
if (isset($data[$i])) $ret[$i] = PMA_convert_display_charset($data[$i]);
if (isset($data[$name])) $ret[PMA_convert_display_charset($name)] = PMA_convert_display_charset($data[$name]);
} else {
/* Meta information available -> check type of field and convert it according to the type */
if (stristr($fields[$i]->type, 'BLOB') || stristr($fields[$i]->type, 'BINARY')) {
if (isset($data[$i])) $ret[$i] = $data[$i];
if (isset($data[$fields[$i]->name])) $ret[PMA_convert_display_charset($fields[$i]->name)] = $data[$fields[$i]->name];
} else {
if (isset($data[$i])) $ret[$i] = PMA_convert_display_charset($data[$i]);
if (isset($data[$fields[$i]->name])) $ret[PMA_convert_display_charset($fields[$i]->name)] = PMA_convert_display_charset($data[$fields[$i]->name]);
}
}
}
return $ret;
}
}
function PMA_DBI_fetch_assoc($result) {
return PMA_mysqli_fetch_array($result, MYSQLI_ASSOC);
}
function PMA_DBI_fetch_row($result) {
return PMA_mysqli_fetch_array($result, MYSQLI_NUM);
}
function PMA_DBI_free_result($result) {
return @mysqli_free_result($result);
}
function PMA_DBI_getError($link = NULL) {
if (empty($link)) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return FALSE;
}
}
$error = mysqli_errno($link);
if ($error && PMA_MYSQL_INT_VERSION >= 40100) {
$error = '#' . ((string) $error) . ' - ' . mysqli_error($link);
} elseif ($error) {
$error = '#' . ((string) $error) . ' - ' . PMA_convert_display_charset(mysqli_error($link));
}
return $error;
}
function PMA_DBI_close($link = NULL) {
if (empty($link)) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return FALSE;
}
}
return @mysqli_close($link);
}
function PMA_DBI_num_rows($result) {
return @mysqli_num_rows($result);
}
function PMA_DBI_insert_id($link) {
if (empty($link)) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return FALSE;
}
}
return mysqli_insert_id($link);
}
function PMA_DBI_affected_rows($link) {
if (empty($link)) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return FALSE;
}
}
return mysqli_affected_rows($link);
}
function PMA_DBI_get_fields_meta($result) {
return mysqli_fetch_fields($result);
}
function PMA_DBI_num_fields($result) {
return mysqli_num_fields($result);
}
?>