Developer Forums | About Us | Site Map
Search  
HOME > PHPMANUAL


Sponsors





Web Host
site hosted by netplex
Partners

Online Manuals

mysqli_fetch_assoc

(PHP 5 CVS only)

mysqli_fetch_assoc -- Fetch a result row as an associative array

Description

array mysqli_fetch_assoc ( resource result)

Returns an associative array that corresponds to the fetched row or FALSE if there are no more rows.

The mysqli_fetch_assoc() function is used to return an associative array representing the next row in the result set for the result represented by the result parameter, where each key in the array represents the name of one of the result set's columns.

If two or more columns in the result set have the same column name, the associative array returned by the mysqli_fetch_assoc() function will contain the value of the last column of that name. If you must work with result sets with this properity, the mysqli_fetch_row() should be used which returns an numerically-indexed array instead.

Note: Field names returned by this function are case-sensitive.

Example 1. An expanded mysqli_fetch_assoc() example

<?php

    $conn = mysqli_connect("localhost", "mysql_user", "mysql_password");
    
    if (!$conn) {
        echo "Unable to connect to DB: " . mysqli_error();
        exit;
    }
    
    if (!mysqli_select_db("mydbname")) {
        echo "Unable to select mydbname: " . mysqli_error();
        exit;
    }
    
    $sql = "SELECT id as userid, fullname, userstatus 
            FROM   sometable
            WHERE  userstatus = 1";

    $result = mysqli_query($sql);

    if (!$result) {
        echo "Could not successfully run query ($sql) from DB: " . mysqli_error();
        exit;
    }
    
    if (mysqli_num_rows($result) == 0) {
        echo "No rows found, nothing to print so am exiting";
        exit;
    }

    // While a row of data exists, put that row in $row as an associative array
    // Note: If you're expecting just one row, no need to use a loop
    // Note: If you put extract($row); inside the following loop, you'll
    //       then create $userid, $fullname, and $userstatus
    while ($row = mysqli_fetch_assoc($result)) {
        echo $row["userid"];
        echo $row["fullname"];
        echo $row["userstatus"];
    }
	    
    mysqli_free_result($result);

 ?>

See also mysqli_fetch_array(), mysqli_fetch_row() and mysqli_fetch_object().

Copyright 2004-2024 GrindingGears.com. All rights reserved. Site hosted by NETPLEX