Movatterモバイル変換


[0]ホーム

URL:


    mysql_fetch_assoc »
    « mysql_escape_string

    mysql_fetch_array

    (PHP 4, PHP 5)

    mysql_fetch_arrayFetch a result row as an associative array, a numeric array, or both

    Warning

    This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.Instead, theMySQLi orPDO_MySQL extension should be used.See alsoMySQL: choosing an API guide.Alternatives to this function include:

    Description

    mysql_fetch_array(resource$result,int$result_type = MYSQL_BOTH):array

    Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

    Parameters

    result

    The resultresource thatis being evaluated. This result comes from a call tomysql_query().

    result_type

    The type of array that is to be fetched. It's a constant and can take the following values:MYSQL_ASSOC,MYSQL_NUM, andMYSQL_BOTH.

    Return Values

    Returns an array of strings that corresponds to the fetched row, orfalse if there are no more rows. The type of returned array depends on howresult_type is defined. By usingMYSQL_BOTH (default), you'll get an array with both associative and number indices. UsingMYSQL_ASSOC, you only get associative indices (asmysql_fetch_assoc() works), usingMYSQL_NUM, you only get number indices (asmysql_fetch_row() works).

    If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you must use the numeric index of the column or make an alias for the column. For aliased columns, you cannot access the contents with the original column name.

    Examples

    Example #1 Query with aliased duplicate field names

    SELECT table1.field AS foo, table2.field AS bar FROM table1, table2

    Example #2mysql_fetch_array() withMYSQL_NUM

    <?php
    mysql_connect
    ("localhost","mysql_user","mysql_password") or
    die(
    "Could not connect: ".mysql_error());
    mysql_select_db("mydb");

    $result=mysql_query("SELECT id, name FROM mytable");

    while (
    $row=mysql_fetch_array($result,MYSQL_NUM)) {
    printf("ID: %s Name: %s",$row[0],$row[1]);
    }

    mysql_free_result($result);
    ?>

    Example #3mysql_fetch_array() withMYSQL_ASSOC

    <?php
    mysql_connect
    ("localhost","mysql_user","mysql_password") or
    die(
    "Could not connect: ".mysql_error());
    mysql_select_db("mydb");

    $result=mysql_query("SELECT id, name FROM mytable");

    while (
    $row=mysql_fetch_array($result,MYSQL_ASSOC)) {
    printf("ID: %s Name: %s",$row["id"],$row["name"]);
    }

    mysql_free_result($result);
    ?>

    Example #4mysql_fetch_array() withMYSQL_BOTH

    <?php
    mysql_connect
    ("localhost","mysql_user","mysql_password") or
    die(
    "Could not connect: ".mysql_error());
    mysql_select_db("mydb");

    $result=mysql_query("SELECT id, name FROM mytable");

    while (
    $row=mysql_fetch_array($result,MYSQL_BOTH)) {
    printf("ID: %s Name: %s",$row[0],$row["name"]);
    }

    mysql_free_result($result);
    ?>

    Notes

    Note:Performance

    An important thing to note is that usingmysql_fetch_array() isnot significantly slower than usingmysql_fetch_row(), while it provides a significant added value.

    Note:Field names returned by this functionarecase-sensitive.

    Note:This function sets NULL fields tothe PHPnull value.

    See Also

    Found A Problem?

    Learn How To Improve This PageSubmit a Pull RequestReport a Bug
    add a note

    User Contributed Notes

    There are no user contributed notes for this page.
    To Top
    and to navigate •Enter to select •Esc to close •/ to open
    PressEnter without selection to search using Google

    [8]ページ先頭

    ©2009-2025 Movatter.jp