---
title: "Arrayproblem"
type: "forum-thread"
url: "https://www.webforum.nu/amne/php/178367-arrayproblem"
topic: "PHP"
topic_url: "https://www.webforum.nu/amne/php"
author: "SoulGod"
published: "2009-05-01T04:35:25.000Z"
updated: "2009-05-03T11:03:35.000Z"
replies: 2
views: 479
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/php/178367-arrayproblem"
---

# Arrayproblem

## #1 — SoulGod, 2009-05-01T04:35Z

Håller nu på att bygga ett "bättre" adminsystem än det jag gjort tidigare,
och kör nu med en array. 
Functionen ser ut såhär (inc_functions.php):

```
function info () {
  $fraga = mysql_query("SELECT * FROM episodes") or exit(mysql_error()); }
    while ($r = mysql_fetch_array($fraga)) {
    $fraga[] = array( "title" => $r[title], "season" =>  $r[s], "episode" =>  $r[e] ); 
    }
```

Och jag hämtar ut infon via (episodes.php):

```
<?PHP 
foreach (info as $r) { ?>

<?php echo $r['title']; ?>
<?php echo $r['season']; ?>
<?php echo $r['episode']; ?>

<?php } ?>
```

Men jag får en bunt med felmeddelanden .. 
Undefined variable: fraga in C:\\EasyPHP\\www\\simpsonslife\\include\\inc_functions.php on line 6

mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\\EasyPHP\\www\\simpsonslife\\include\\inc_functions.php on line 6

Use of undefined constant info - assumed 'info' in C:\\EasyPHP\\www\\simpsonslife\\episodes.php on line 4

Invalid argument supplied for foreach() in C:\\EasyPHP\\www\\simpsonslife\\episodes.php on line 4

Permalänk: https://www.webforum.nu/p/178367

## #2 — drew, 2009-05-01T10:35Z

> ```
> function info()
> {
>   $fraga = mysql_query("SELECT * FROM episodes") or exit(mysql_error());
> }
> 
> while ($r = mysql_fetch_array($fraga))
> {
>   $fraga[] = array( "title" => $r[title], "season" =>  $r[s], "episode" =>  $r[e] ); 
> }
> ```

Du borde läsa på lite om indentering. Indentering gör din kod lättare att både läsa och felsöka. Jag indenterade om din kod
$fraga är inte satt när du kör din while-sats

> ```
> <?PHP 
> foreach (info as $r) { ?>
> 
> <?php echo $r['title']; ?>
> <?php echo $r['season']; ?>
> <?php echo $r['episode']; ?>
> 
> <?php } ?>
> ```

Vad är detta för något? Tänkte du verkligen när du skrev det här?

```
<?php
foreach (info as $r)
{ 
  echo $r['title'];
  echo $r['season'];
  echo $r['episode'];
}
?>
```

Du har en funktion som du kallar info. foreach stegar genom en array.

Permalänk: https://www.webforum.nu/p/2183704

## #3 — Dies_Irae, 2009-05-03T11:03Z

Du ändra info i foreach till info() så funktionen anropas och sen returnera $fraga.

Permalänk: https://www.webforum.nu/p/2183797

---

Tråden på webben: https://www.webforum.nu/amne/php/178367-arrayproblem
