Undefined index on array output

Ask about a PHP problem here.
Post Reply
wrichards8
Posts: 66
Joined: Thu Jan 12, 2012 3:54 pm
Contact:

Undefined index on array output

Post by wrichards8 »

Hi,

I just wondered if you can help me. I have a function which returns an array [syntax=php]<?php function topicname()
{
$querylist = mysql_query("SELECT `topicid`,`topicname` FROM `topiclist`");
$topiclist = array();
while ($listresult = mysql_fetch_assoc($querylist))
{
$topiclist[] = $listresult;
}
return $topiclist;
}
?>[/syntax]

My problem is that when I call the function and feed the array into another function
[syntax=php]$topic = topicname();
print_r($topic);
print_r(forumdetails($topic));[/syntax]
It says Notice: Undefined index: topicid in D:\xamppstuff\htdocs\flamebox\internal\func\forumstuff-inc.php on line 14

I just wondered what I am doing wrong. I have my variable defined as [syntax=php] $topicid = (int)$listresult['topicid'];[/syntax] so am not sure why it gives me the error
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Undefined index on array output

Post by Temor »

which line is line 14?
wrichards8
Posts: 66
Joined: Thu Jan 12, 2012 3:54 pm
Contact:

Re: Undefined index on array output

Post by wrichards8 »

Line 14 was this line [syntax=php] $topicid = (int)$listresult['topicid'];
[/syntax] but I have changed it so now the value is assigned to a variable which is passed to the other function (so now not using the array as input)
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Undefined index on array output

Post by EcazS »

[syntax=php]$topicid = (int)$listresult['topicid'];[/syntax]
That's the problem. You are putting the results into the topiclist array in the while loop.
Post Reply