Undefined index error - using PHP with MySQL

I know questions about the “undefined index error” have been posted before, but I couldn’t find any that seemed to hit on the same problem I am having.

I am retrieving some records from a mysql database and putting them into an array. I then want to use the array values in different locations in my web page. But, the only way I seem to be able to use the array values is in a foreach loop. This is inconvenient if I want to use the different values in different locations. When I try to output one of the array values outside of a foreach loop, I get an undefined index error message.

This is a piece of the code I am using.

 // Records retrieved.  Put them in an array
    while($row = $result->fetch_assoc()){
    $DataRetrieved[] = array(

//output some data - 1st try
  foreach ($DataRetrieved as $entry):
    echo 'Business name: '.$entry['busName'];

//output some data - 2nd try
  echo 'Business name: '.$DataRetrieved['busName'];

The output from the 1st try is “Business name: mybusiness”.

The output from the 2nd try is “Notice: undefined index: busName …”.

Once the results from the query are put into an array ($DataRetrieved in this case), shouldn’t I be able to retrieve any value from the array?

As usual, I am sure I am making some stupid newbie mistake, but if someone could point it out to me I could stop pulling out my hair.

How are you determining which record of data you want to display at any location? Unless you set the array indexes to something that you can use to reference an individual record in the array, you are limited to looping over the records, directly referencing the first/last one, or picking random entries.


In a nutshell I am creating a report with business information. So on some line of the report I want to say (for example), “Business name: my business”. To do that I would think my code could be,

<p>Business name: <?php $DataRetrieved['busName']; ?> </p>

But instead, it seems I have to say,

<p>Business name: <?php foreach ($DataRetrieved as $entry){echo ($entry["busName"]);} ?> </p>

You say “Unless you set the array indexes to something that you can use to reference an individual record in the array”. I thought that was what I was doing when I assigned the results from the query to the array $DataRetrieved. I’m confused why I cannot refer to the $DataRetrieved array directly, why I need to use a foreach loop.


For your example, where you want to display common information from the rows of data, you would just reference the zeroth element in the array -

echo $DataRetrieved[0]['busName'];

Perhaps if you examine what the array looks like -

echo '<pre>'; print_r($DataRetrieved); echo '</pre>';

OK. Figured it out. When I assign the results to the array, it is assuming I have retrieved multiple records. But I only request one record. So, instead of retrieving a single value using,

<p>Business name: <?php $DataRetrieved['busName']; ?> </p>

I need to use,

<p>Business name: <?php $DataRetrieved[0]['busName']; ?> </p>

since this is the first, and only, record.


Then, just fetch the single row into an appropriately named variable. Don’t use a loop to fetch the data and don’t use a loop to display the data.

// fetch the single row of data
$DataRetrieved = $result->fetch_assoc();

// at the point of using an element in the fetched row of data
echo $DataRetrieved['busName'];

OK. Thanks. I’ll try that approach.

Make the primary array KEY the record ID so it can be used or called anywhere.
Define the variable as an array before the while then id as the key and the $row as the value of fields.

	$DataRetrieved = array();
	while($row = $result->fetch_assoc()){
		$DataRetrieved[$row['id']] = $row;

If you know an ID you call any fields you wish, e.g.

echo 'Business name: '.$DataRetrieved[$id]['busName'];

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.