mysql_result
(PHP 4, PHP 5)
mysql_result — Get result data
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
- mysqli_data_seek() in conjunction with mysqli_field_seek() and mysqli_fetch_field()
- PDOStatement::fetchColumn()
Description
Retrieves the contents of one cell from a MySQL result set.
When working on large result sets, you should consider using one of the functions that fetch an entire row (specified below). As these functions return the contents of multiple cells in one function call, they're MUCH quicker than mysql_result(). Also, note that specifying a numeric offset for the field argument is much quicker than specifying a fieldname or tablename.fieldname argument.
Parameters
-
result
-
The result resource that is being evaluated. This result comes from a call to mysql_query().
-
row
-
The row number from the result that's being retrieved. Row numbers start at 0.
-
field
-
The name or offset of the field being retrieved.
It can be the field's offset, the field's name, or the field's table dot field name (tablename.fieldname). If the column name has been aliased ('select foo as bar from...'), use the alias instead of the column name. If undefined, the first field is retrieved.
Return Values
The contents of one cell from a MySQL result set on success, or
FALSE
on failure.
Examples
Example #1 mysql_result() example
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db('database_name')) {
die('Could not select database: ' . mysql_error());
}
$result = mysql_query('SELECT name FROM work.employee');
if (!$result) {
die('Could not query:' . mysql_error());
}
echo mysql_result($result, 2); // outputs third employee's name
mysql_close($link);
?>
Notes
Note:
Calls to mysql_result() should not be mixed with calls to other functions that deal with the result set.
See Also
- mysql_fetch_row() - Get a result row as an enumerated array
- mysql_fetch_array() - Fetch a result row as an associative array, a numeric array, or both
- mysql_fetch_assoc() - Fetch a result row as an associative array
- mysql_fetch_object() - Fetch a result row as an object
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- MySQL Drivers and Plugins
- Оригинальное API MySQL
- mysql_affected_rows
- mysql_client_encoding
- mysql_close
- mysql_connect
- mysql_create_db
- mysql_data_seek
- mysql_db_name
- mysql_db_query
- mysql_drop_db
- mysql_errno
- mysql_error
- mysql_escape_string
- mysql_fetch_array
- mysql_fetch_assoc
- mysql_fetch_field
- mysql_fetch_lengths
- mysql_fetch_object
- mysql_fetch_row
- mysql_field_flags
- mysql_field_len
- mysql_field_name
- mysql_field_seek
- mysql_field_table
- mysql_field_type
- mysql_free_result
- mysql_get_client_info
- mysql_get_host_info
- mysql_get_proto_info
- mysql_get_server_info
- mysql_info
- mysql_insert_id
- mysql_list_dbs
- mysql_list_fields
- mysql_list_processes
- mysql_list_tables
- mysql_num_fields
- mysql_num_rows
- mysql_pconnect
- mysql_ping
- mysql_query
- mysql_real_escape_string
- mysql_result
- mysql_select_db
- mysql_set_charset
- mysql_stat
- mysql_tablename
- mysql_thread_id
- mysql_unbuffered_query
Коментарии
here's a rough replacement using mysqli:
if (!function_exists('mysql_result')) {
function mysql_result($result, $number, $field=0) {
mysqli_data_seek($result, $number);
$row = mysqli_fetch_array($result);
return $row[$field];
}
}
I was also looking for mysqli replacement for getting first row, when query is known to return just 1 value. It seems this is simple:
echo(mysql_result($result, 0));
becomes
echo($result->fetch_row()[0]);
alternatively
if ($row = $result->fetch_row()) {
echo($row[0]);
}
This function accounts for the ability of the original function to accept "table_name.field_name" entries as well as just the field name:
function mysqli_result($res, $row, $field=0) {
$result->data_seek($row);
$data = $result->fetch_array();
$field_name = $field;
if ( is_string($field) ) {
$fields_array = explode(".", $field);
$array_len = count($fields_array);
if ($array_len > 1) {
$field_name = $fields_array[array_len - 1];
} else {
$field_name = $fields_array[0];
}
}
return $data[$field_name];
}
To replace a row count e.g.
$querystring = "SELECT COUNT(*) FROM table WHERE column1 = 'EXAMPLE'";
$total_records = mysql_result(mysql_query($querystring,$connection),0);
use mysqli_fetch_row :
$querystring = "SELECT COUNT(*) FROM table WHERE column1 = 'EXAMPLE'";
$rowcount = mysqli_fetch_row(mysqli_query($connection,$querystring));
$total_records = $rowcount[0];