Forum Moderators: coopster

Message Too Old, No Replies

MYSQL results to arrays

         

mooger35

6:04 pm on Feb 20, 2009 (gmt 0)

10+ Year Member



For some reason I'm coming up with blank arrays after pulling data from my database. I usually have no issues with this but I'm obviously missing something simple.

I don't use variable variables very often, could my problem be there?

$team1 = array();
$team2 = array();
$team3 = array();
$team4 = array();
$team5 = array();
$team6 = array();
$team7 = array();
$team8 = array();
$team9 = array();
$team10 = array();

for($i=1;$i<11;$i++){
$team = "Team $i";
$var = "team$i";
$sql = mysql_query("SELECT gamedate, clock, IF(home = '$team', 'home', 'away') AS homeaway
FROM temp_schedule
WHERE home = '$team'
OR visitor = '$team'
ORDER BY gamedate ASC") OR die(mysql_error());

$d=1;
while($row = mysql_fetch_assoc($sql)){
$$var[$team]['game'] = $d;
$$var[$team]['team'] = $team;
$$var[$team]['date'] = $row['gamedate'];
$$var[$team]['time'] = $row['clock'];
$$var[$team]['homeaway'] = $row['homeaway'];

$d++;
}
}
echo "<pre>";
for($i=1;$i<11;$i++){
$show = "team$i";
print_r($$show);
}
echo "</pre>";

mooger35

6:51 pm on Feb 20, 2009 (gmt 0)

10+ Year Member



Nevermind... I made that way more complicated than I needed to. I also realize now that it was the variable variables. In the end, I didn't really need them for the arrays anyway.

jojy

10:41 pm on Feb 21, 2009 (gmt 0)

10+ Year Member



here is simple solution

$link = mysql_query("SELECT * FROM table");
$data = array();
while($result = mysql_fetch_assoc($link)) {
$data[] = $result;
}

foreach($data as $value){
echo $value['field'];
}

simple and efficient ...