Read data from mysqli_fetch_array like a multidimensional array?

前端 未结 3 1858
执念已碎
执念已碎 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:27

    It depends how you are returning your results from the database.

    there are flags in your mysqli_fetch_array function which you can set to modify your returned result.

    If you use $row = mysqli_fetch_array($result, MYSQLI_ASSOC); or in OOP $row = $result->fetch_array(MYSQLI_ASSOC);

    then you can access your returned result as column name in your while loop like $row['name'] or $row['age']

    Or if you use $row = mysqli_fetch_array($result, MYSQLI_NUM); or in OOP $row = $result->fetch_array(MYSQLI_NUM);

    then you can access your returned result in while loop like $row[0] or $row[3]

    Simple example would be

    while($row = mysqli_fetch_array($result)) {
        echo $row['name'] . " " . $row['age'];
    }
    

    For further information. Read PHP.net Fetch Array

提交回复
热议问题