Read data from mysqli_fetch_array like a multidimensional array?

前端 未结 3 1833
执念已碎
执念已碎 2021-01-23 23:58

When I use mysqli_fetch_array() I get an array, but how do I read the values? Is a While-Loop the only option, or can I pick any value from a row and column like a

3条回答
  •  野的像风
    2021-01-24 00:26

    while loop fetches you a row per iteration.

    You can get all rows as multidimensional array with mysqli_fetch_all

    After that you can use pick your values with [rowNum][colNum]

    But beware when your result has lot of rows - array can be very big and cause memory or other issues.

    Update to clarify:

    if you want to receive multidimensional array of rows there are two ways:

    First: iterate over mysqli_result with fetch_assoc/fetch_array and append row to array:

    $rows = array();
    while ($row = mysqli_fetch_array($res)) {
        $rows[] = $row;
    }
    echo $rows[0]['whatever'];
    

    Second: receive all results with one function call:

    $rows = mysqli_fetch_all($res, MYSQLI_ASSOC);
    echo $rows[0]['whatever'];
    

    That's all. No more methods are available.

提交回复
热议问题