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


i testing out data set looking return db. running in command line mode. when var_dump() data, can see data being returned, when try traverse array, has duplicate data in it, warning message below , can not print array item.

i sure obvious some, not know why happening. sure doing wrong here...but what?

consider:

$link = mysqli_connect("localhost","username","password","mydatabase") or die("error " . mysqli_error($link));  $query = "select * citizen_application"; $execute = $query or die("error in consult.." . mysqli_error($link));   //execute query.  $result = $link->query($execute);  $data = mysqli_fetch_array($result); // tried mysqli_fetch_assoc() , issue persists  //display information:  //var_dump($data); //this show duplicates in array returned???  foreach($data $data_unit){     print_r($data_unit["dob"]."\r"); } 

the warning in logs:

illegal string offset 'dob'

mysqli_fetch_array returns array, you're traversing array foreach, $data_unit single element , not array... try just

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

or use mysqli_fetch_assoc() , try

foreach($data $fieldname => $data_unit){     echo "$fieldname = $data_unit\r"; } 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -