Fetch a result row as an associative array in PHP

Description

The following code shows how to fetch a result row as an associative array.

Example


/*from  w w w .jav  a2s .  c om*/
<?php
    $con=mysqli_connect("localhost","root","","test");
    // Check connection
    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $sql="SELECT name,birthday FROM employee";
    $result=mysqli_query($con,$sql);

    // Associative array
    $row=mysqli_fetch_assoc($result);
    printf ("%s (%s)\n",$row["name"],$row["birthday"]);

    // Free result set
    mysqli_free_result($result);

    mysqli_close($con);
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    MySQL »




MySQLi
MySQLi Object Oriented