mysqli_result ă‚Żăƒ©ă‚č

(PHP 5, PHP 7, PHP 8)

ăŻă˜ă‚ă«

ăƒ‡ăƒŒă‚żăƒ™ăƒŒă‚čまぼクスăƒȘă«ă‚ˆă‚ŠćŸ—ă‚‰ă‚ŒăŸç”æžœă‚»ăƒƒăƒˆă‚’èĄšă—ăŸă™ă€‚

ă‚Żăƒ©ă‚č抂芁

class mysqli_result implements IteratorAggregate {
/* プロパティ */
public readonly int $current_field;
public readonly int $field_count;
public readonly ?array $lengths;
public readonly int|string $num_rows;
public int $type;
/* ăƒĄă‚œăƒƒăƒ‰ */
public function __construct(mysqli $mysql, int $result_mode = MYSQLI_STORE_RESULT)
public function data_seek(int $offset): bool
public function fetch_all(int $mode = MYSQLI_NUM): array
public function fetch_array(int $mode = MYSQLI_BOTH): array|null|false
public function fetch_assoc(): array|null|false
public function fetch_column(int $column = 0): null|int|float|string|false
public function fetch_field(): object|false
public function fetch_field_direct(int $index): object|false
public function fetch_fields(): array
public function fetch_object(string $class = "stdClass", array $constructor_args = []): object|null|false
public function fetch_row(): array|null|false
public function field_seek(int $index): true
public function free(): void
public function close(): void
public function free_result(): void
public function getIterator(): Iterator
}

プロパティ

type

ç”æžœă‚»ăƒƒăƒˆă‚’ăƒăƒƒăƒ•ă‚ĄăƒȘングするか、しăȘă„ă‹ă‚’æ•Žæ•°ă§èš­ćźšă—ăŸă™ă€‚ (バッファăƒȘングする栮搈は MYSQLI_STORE_RESULT を、 しăȘい栮搈は MYSQLI_USE_RESULT を ăă‚Œăžă‚Œèš­ćźšă—ăŸă™)

ć€‰æ›Žć±„æ­Ž

ăƒăƒŒă‚žăƒ§ăƒł èȘŹæ˜Ž
8.0.0 mysqli_result は、 IteratorAggregate ă‚’ćźŸèŁ…ă™ă‚‹ă‚ˆă†ă«ăȘă‚ŠăŸă—ăŸă€‚ ă“ă‚Œă‚ˆă‚Šć‰ăźăƒăƒŒă‚žăƒ§ăƒłă§ăŻă€ Traversable ă‚’ćźŸèŁ…ă—ăŠă„ăŸă—ăŸă€‚

ç›źæŹĄ

  • mysqli_result::__construct — mysqli_result ă‚Șăƒ–ă‚žă‚§ă‚Żăƒˆă‚’æ§‹çŻ‰ă™ă‚‹
  • mysqli_result::$current_field — ç”æžœăƒă‚€ăƒłă‚żă«ăŠă‘ă‚‹çŸćœšăźăƒ•ă‚ŁăƒŒăƒ«ăƒ‰ă‚Șăƒ•ă‚»ăƒƒăƒˆă‚’ć–ćŸ—ă™ă‚‹
  • mysqli_result::data_seek — ç”æžœăźä»»æ„ăźèĄŒă«ăƒă‚€ăƒłă‚żă‚’ç§»ć‹•ă™ă‚‹
  • mysqli_result::fetch_all — ç”æžœăźă™ăčăŠăźèĄŒă‚’é€Łæƒłé…ćˆ—ăƒ»æ•°ć€€æ·»ć­—é…ćˆ—ă‚ă‚‹ă„ăŻăăźäžĄæ–čăźćœąćŒă§ć–ćŸ—ă™ă‚‹
  • mysqli_result::fetch_array — ç”æžœă‚»ăƒƒăƒˆăźæŹĄăźèĄŒă‚’é€Łæƒłé…ćˆ—ăƒ»æ•°ć€€æ·»ć­—é…ćˆ—ă‚ă‚‹ă„ăŻăăźäžĄæ–čăźćœąćŒă§ć–ćŸ—ă™ă‚‹
  • mysqli_result::fetch_assoc — ç”æžœă‚»ăƒƒăƒˆăźæŹĄăźèĄŒă‚’é€Łæƒłé…ćˆ—ă§ć–ćŸ—ă™ă‚‹
  • mysqli_result::fetch_column — ç”æžœă‚»ăƒƒăƒˆăźæŹĄăźèĄŒă‹ă‚‰ă€ć˜äž€ăźă‚«ăƒ©ăƒ ăźć€€ă‚’ć–ćŸ—ă™ă‚‹
  • mysqli_result::fetch_field — ç”æžœă‚»ăƒƒăƒˆăźæŹĄăźăƒ•ă‚ŁăƒŒăƒ«ăƒ‰ă‚’èż”ă™
  • mysqli_result::fetch_field_direct — ć˜äž€ăźăƒ•ă‚ŁăƒŒăƒ«ăƒ‰ăźăƒĄă‚żăƒ‡ăƒŒă‚żă‚’ć–ćŸ—ă™ă‚‹
  • mysqli_result::fetch_fields — ç”æžœă‚»ăƒƒăƒˆăźăƒ•ă‚ŁăƒŒăƒ«ăƒ‰æƒ…ć ±ă‚’ă‚Șăƒ–ă‚žă‚§ă‚Żăƒˆăźé…ćˆ—ă§èż”ă™
  • mysqli_result::fetch_object — ç”æžœă‚»ăƒƒăƒˆăźæŹĄăźèĄŒă‚’ć–ćŸ—ă—ă€ă‚Șăƒ–ă‚žă‚§ă‚Żăƒˆăšă—ăŠèż”ă™
  • mysqli_result::fetch_row — ç”æžœă‚»ăƒƒăƒˆăźæŹĄăźèĄŒă‚’æ•°ć€€æ·»ć­—é…ćˆ—ă§ć–ćŸ—ă™ă‚‹
  • mysqli_result::$field_count — ç”æžœă‚»ăƒƒăƒˆăźăƒ•ă‚ŁăƒŒăƒ«ăƒ‰æ•°ă‚’ć–ćŸ—ă™ă‚‹
  • mysqli_result::field_seek — ç”æžœăƒă‚€ăƒłă‚żă‚’ă€æŒ‡ćźšă—ăŸăƒ•ă‚ŁăƒŒăƒ«ăƒ‰ă‚Șăƒ•ă‚»ăƒƒăƒˆă«èš­ćźšă™ă‚‹
  • mysqli_result::free — ç”æžœă«é–ąé€Łä»˜ă‘ă‚‰ă‚ŒăŸăƒĄăƒąăƒȘă‚’è§Łæ”Ÿă™ă‚‹
  • mysqli_result::getIterator — ć€–éƒšă‚€ăƒ†ăƒŹăƒŒă‚żă‚’ć–ćŸ—ă™ă‚‹
  • mysqli_result::$lengths — ç”æžœă‚»ăƒƒăƒˆă«ăŠă‘ă‚‹çŸćœšăźèĄŒăźă‚«ăƒ©ăƒ ăźé•·ă•ă‚’èż”ă™
  • mysqli_result::$num_rows — ç”æžœă‚»ăƒƒăƒˆăźèĄŒæ•°ă‚’ć–ćŸ—ă™ă‚‹
add a note

User Contributed Notes 4 notes

up
65
tuxedobob ¶
14 years ago
Converting an old project from using the mysql extension to the mysqli extension, I found the most annoying change to be the lack of a corresponding mysql_result function in mysqli. While mysql_result is a generally terrible function, it was useful for fetching a single result field *value* from a result set (for example, if looking up a user's ID).

The behavior of mysql_result is approximated here, though you may want to name it something other than mysqli_result so as to avoid thinking it's an actual, built-in function.

<?php
function mysqli_result($res, $row, $field=0) {
    $res->data_seek($row);
    $datarow = $res->fetch_array();
    return $datarow[$field];
}
?>

Implementing it via the OO interface is left as an exercise to the reader.
up
19
Cem Kayali / cemkayali(a)eticaret.com.tr ¶
7 years ago
Switching from Php5 to Php7, especially if you have worked on an ongoing, long term project, it is unfortunate that there is no mysqli_result function.

So, this may be helpfull and you can call this function as you wish. I assume you do restricted search (searching for single row or few rows only).

function mysqli_result($search, $row, $field){
$i=0; while($results=mysqli_fetch_array($search)){
if ($i==$row){$result=$results[$field];}
$i++;}
return $result;} 

Simply;

$search=mysqli_query($connection, "select name from table_name where id='7'");
$name=mysqli_result($search, 0, "id"); 

Best wishes,
up
15
Marc17 ¶
12 years ago
An "mysqli_result" function where $field can be like table_name.field_name with alias or not.
<?php
function mysqli_result($result,$row,$field=0) {
    if ($result===false) return false;
    if ($row>=mysqli_num_rows($result)) return false;
    if (is_string($field) && !(strpos($field,".")===false)) {
        $t_field=explode(".",$field);
        $field=-1;
        $t_fields=mysqli_fetch_fields($result);
        for ($id=0;$id<mysqli_num_fields($result);$id++) {
            if ($t_fields[$id]->table==$t_field[0] && $t_fields[$id]->name==$t_field[1]) {
                $field=$id;
                break;
            }
        }
        if ($field==-1) return false;
    }
    mysqli_data_seek($result,$row);
    $line=mysqli_fetch_array($result);
    return isset($line[$field])?$line[$field]:false;
}
?>
up
-2
blar at blar dot de ¶
17 years ago
Extending the MySQLi_Result

<?php

class Database_MySQLi extends MySQLi
{
    public function query($query)
    {
        $this->real_query($query);
        return new Database_MySQLi_Result($this);
    }
}

class Database_MySQLi_Result extends MySQLi_Result
{
    public function fetch()
    {
        return $this->fetch_assoc();
    }

    public function fetchAll()
    {
        $rows = array();
        while($row = $this->fetch())
        {
            $rows[] = $row;
        }
        return $rows;
    }
}

?>