CakeFest 2024: The Official CakePHP Conference

mysql_result

(PHP 4, PHP 5)

mysql_result結果データを得る

警告

この拡張モジュールは PHP 5.5.0 で非推奨になり、PHP 7.0.0 で削除されました。 MySQLi あるいは PDO_MySQL を使うべきです。詳細な情報は MySQL: API の選択 を参照ください。 この関数の代替として、これらが使えます。

説明

mysql_result(resource $result, int $row, mixed $field = 0): string

MySQL の結果セットからひとつのセルの内容を取得します。

大量の結果セットで作業を行う際は、行全体を取り込む関数のうちひとつを 使用することを検討するべきです(以下で説明します)。 これらの関数は一回の関数コールで複数のセルの内容を返すので、 mysql_result() よりもかなり高速です。 また、フィールド引数としてオフセット数値を指定する方が フィールド名やテーブル名.フィールド名のように指定するよりも かなり高速です。

パラメータ

result

評価された結果 リソース。この結果は、mysql_query() のコールにより得られたものです。

row

結果から取得する行の番号。行番号は 0 からはじまります。

field

取得したいフィールド名またはフィールドのオフセット。

フィールドのオフセット、フィールド名またはテーブル名.フィールド名を 指定可能です。カラム名のエイリアスが定義されている ('select foo as bar from...')場合、そのカラム名の代わりに エイリアスを使用してください。指定しなかった場合は最初のフィールドを 取得します。

戻り値

成功した場合に MySQL 結果セットのひとつのセルの内容、 失敗した場合に false を返します。

例1 mysql_result() の例

<?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); // 3 番目の employee の name を出力する

mysql_close($link);
?>

注意

注意:

mysql_result() は、 結果セットを処理するほかの関数と混用することはできません。

参考

add a note

User Contributed Notes 8 notes

up
18
freedman at FreeFormIT dot com
9 years ago
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];
}
}
up
1
mark at pnod dot co dot uk
1 year ago
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];
up
0
bob dot schuon at loungelizard dot com
4 years ago
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];
}
up
-3
v dot teemu at gmail dot com
7 years ago
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]);
}
up
-8
harmmeiier at gmail dot com
9 years ago
An example of how to easily port this to mysqli would be nice instead of some links to functions that don't really do what this function does.
up
-9
adam dot chou at gmail dot com
15 years ago
mysql_result() will throw E_WARNING if mysql_query returns 0 rows. This is unlike any of the mysql_fetch_* functions so be careful of this if you have E_WARNING turned on in error_reporting(). You might want to check mysql_num_rows() before calling mysql_result()
up
-20
bruce at kaskubar dot com
12 years ago
The warning against mixing the use of mysql_result with other result set functions is a bit generic. More specifically, mysql_result alters the result set's internal row pointer (at least in a LAMP environment). This is anything but obvious as the nature of the function is random access for grabbing a quick byte. Using mysql_data_seek after some mysql_result calls, before going into a mysql_fetch_array loop, will set things straight.
up
-31
raz0 at NOSPAM dot worldonline dot dk
20 years ago
If you want to fetch the result from a mysql query similar to one of these two queries...

$query = mysql_query("SELECT COUNT(*) FROM table");
$query = mysql_query("SELECT LAST_INSERT_ID()");

... you would use mysql_result() like shown below to retrieve the output as an int.

$result = mysql_result($query, 0, 0);
To Top