1

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 I try to traverse the array, which has duplicate data in it, I get the warning message below and can not print the array item.

I am sure to some that is obvious to some, but I do not know why this is happening. I am sure I am doing something wrong here...but what?

Consider:

$link = mysqli_connect("localhost","username","password","mydatabase") or die("Error " . mysqli_error($link)); 
$query = "SELECT * FROM citizen_application";
$execute = $query or die("Error in the consult.." . mysqli_error($link)); 

//execute the query. 
$result = $link->query($execute); 
$data = mysqli_fetch_array($result); // also tried mysqli_fetch_assoc() and the issue persists

//display information: 
//var_dump($data); //This show duplicates in the array returned???

foreach($data as $data_unit){
    print_r($data_unit["dob"]."\r");
}

The warning in the logs:

Illegal string offset 'dob'

2
  • Have you tried print_r($data) and then checked to see where the array['dob'] array entry is? 'var_dump' can be 'confusing' when dealing with 'recursive' structures. imo, print_r makes it clearer sometimes. Commented Apr 24, 2015 at 21:25
  • Indeed I have, at least using mysqli_fetch_assoc() and DOB is there at the end. Commented Apr 24, 2015 at 21:30

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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";
}

2 Comments

Not quite, but the mysqli_fetch_assoc() did remove the duplicates in my var_dump(). I want to get at the DOB data specifically and I do not see that in your answer.
You were using foreach in your example, you did not indicate this was the only field of interest, the foreach is inappropriate for the results your clarifying comment indicated, in that case replace the entire foreach stanza with "echo $data["dob"];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.