Вы находитесь на странице: 1из 4

Dealing With the More Difficult Methods

The bad news, not all methods are easy to migrate as the ones listed above. The good news,
these methods are not that common so you may not even have to deal with them. These more
difficult methods will require some discussion, so we will go through them one at a time.

mysql_client_encoding -> mysqli_character_set_name( $link)

This is a simple name change.

mysql_create_db

This statement is replaced with the mysqli_query method using the CREATE DATABASE sql...

$result = mysqli_query( $link, 'CREATE DATABASE database_name' );

mysql_db_name

This statement is used in conjunction with the mysql_list_dbs statement to get the requested row
from a given result. To migrate it to MySQLi, we have to use the mysqli_data_seek method to
locate the requested row and then mysqli_fetch_row to return requested row.

$data = mysql_db_name( $result, $row);

becomes

mysqli_data_seek( $result, $row);


$fetch = mysql_fetch_row( $result );
$data = $fetch[0];

mysql_db_query

In MySQL, this statement selects a database and runs the query. To migrate it to MySQLi, we
use the mysqli_select_db method to select the database and then the mysqli_query method to
run the query and return the result.

$result = mysql_db_query( 'database', 'query');

becomes

mysqli_select_db( 'database' );
$result = mysqli_query( 'query' );

mysql_drop_db

This statement is replaced with the mysqli_query method using the DROP DATABASE sql...

$result = mysqli_query( $link, 'DROP DATABASE database_name');

mysql_escape_string -> mysql_real_escape_string( $link, 'string')

This is a simple name change.


mysql_fetch_field -> mysqli_fetch_field( $result )

If this statement does not contain the optional offset parameter, then it is a simple name
replacement to migrate. If the offset parameter is included, then we have to loop through the
result until we find the requested offset.

$fetch = mysql_fetch_field( $result, 5);

becomes

for(x=0; x<5; x++) {


mysqli_fetch_field( $result );
}
$fetch = mysqli_fetch_field( $result );

mysql_field_len
mysql_field_name
mysql_field_table

In MySQL, these statements return the length, name or table of the specified field. To migrate it
we use the MySQLi method mysqli_fetch_field_direct to return an object containing the field data
and then return the field length, name or table from that object.

$length = mysql_field_len( $result, 5);


$name = mysql_field_name( $result, 5);
$table = mysql_field_table( $result, 5)

becomes

$fieldInfo = mysqli_fetch_field_direct( $result, 5);


$length = $fieldInfo->length;
$name = $fieldInfo->name;
$table = $fieldInfo->table;

mysql_list_dbs

This statement is replaced with the mysqli_query method using the SHOW DATABASES sql...

$result = mysqli_query( $link, 'SHOW DATABASES');

mysql_list_fields

This statement is replaced with the mysqli_query method using the SHOW COLUMNS FROM
sql...

$result = mysqli_query( $link, 'SHOW COLUMNS FROM table_name' );

mysql_list_processes -> mysqli_thread_id( $link )

This is a simple name change.

mysql_list_tables

This statement is replaced with the mysqli_query method using the SHOW TABLES FROM sql...
$result = mysqli_query( $link, 'SHOW TABLES FROM database_name');

mysql_num_fields -> mysqli_field_count( $link )

This statement references the result in MySQL and is replaced with the mysql_field_count
method which references the link.

mysql_result

In MySQL, this statement fetches a specified row and optional field from a given result. To
migrate it we use the mysqli_data_seek to locate the row and loop through the fields using
mysqli_fetch_field to return the field.

$fetch = mysql_result( $result, 3, 'field');

becomes

mysql_data_seek($result, 3);
if( !empty($field) ) {
while($finfo = mysqli_fetch_field( $result )) {
if( $field == $finfo->name ) {
$f = mysqli_fetch_assoc( $result );
$fetch = $f[ $field ];
}
}
} else {
$f = mysqli_fetch_array( $result );
$fetch = $f[0];
}

mysql_tablename

In MySQL, this statement returns the table name in the row of a specified result. To migrate it we
use the mysqli_data_seek method to locate the specified row and fetch the name using the
mysqli_fetch_array method.

$name = mysql_tablename( $result, 3 );

becomes

mysqli_data_seek( $result, 3 );
$f = mysql_fetch_array( $result );
$fetch = $f[0];

mysql_unbuffered_query

This statement is replaced with the mysqli_query method with the result mode set to
MYSQLI_USE_RESULT...

$result = mysqli_query($link, 'query', MYSQLI_USE_RESULT);

The Red-Headed Step-Children


First off I would like to point out that I have nothing against red-headed people or step-children in
general. In the United States this is a phrase which refers to a problem that nobody wants to deal
with and I just do not know what a politically correct replacement would be, so we are stuck with
it.

There are 2 MySQL statements that are a real pain to deal with since they use flags and types
that are not supported in MySQLi the same way they where in MySQL. To get these to work, we
have to create our own.

mysql_field_flags

$resultFlags = mysql_field_flags( $result, 3);

becomes

$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];
$flags_num = mysqli_fetch_field_direct( $result, $field_offset )-
>flags;
$result = array();
foreach ($flags as $n => $t)
if ($flags_num & $n)
$result[] = $t;
$returnFlags = implode(' ', $result);
$returnFlags = str_replace( 'PRI_KEY', 'PRIMARY_KEY', $returnFlags);
$returnFlags = strtolower($returnFlags);
}

mysql_field_type

$resultType = mysql_field_type( $result, 4);

becomes

$type_id = mysqli_fetch_field_direct( $result, $field_offset)->type;


$types = array();
$constants = get_defined_constants(true);
foreach ($constants['mysqli'] as $c => $n)
if (preg_match('/^MYSQLI_TYPE_(.*)/', $c, $m))
$types[$n] = $m[1];
$resultType = array_key_exists( $type_id, $types ) ? $types[$type_id] :
NULL;

Source: phppackage

Shareby : sedotcode