Strange Illegal string offset in foreach from mysqli_fetch_array() and mysqli_fetch_assoc()

前端 未结 2 582
不知归路
不知归路 2021-01-25 08:59

I am just testing out a data set I am looking to return from the DB. I am running this in command line mode. When I var_dump() the data, I can see data being returned, but when

相关标签:
2条回答
  • 2021-01-25 09:36

    There seems to be no way to do this with a foreach() when running the script in command line mode. But I found a solution below that gives me what I was looking for:

    while($data = mysqli_fetch_assoc($result)) { 
       print_r($data["dob"]."\n");
    } 
    

    I noticed all the examples in the documentation where doing this way. I thought it was just a preference. It does not seem so. I hope this helps someone else, because this was quite irritating. You used to be able to do this easily with the previous mysql functions.

    0 讨论(0)
  • 2021-01-25 09:50

    mysqli_fetch_array returns an array, you're traversing the array with the foreach, $data_unit will most likely be a single element and not an array... try just

    foreach($data as $data_unit){
        echo $data_unit."\r";
    }
    

    or use mysqli_fetch_assoc() and try

    foreach($data as $fieldname => $data_unit){
        echo "$fieldname = $data_unit\r";
    }
    
    0 讨论(0)
提交回复
热议问题