0

I currently have an array that stores the values of one column from the database and that works fine however I want to store more than one column value. So in my example I want a team and their venue stored in the same array index. I can't seem find a way to do this.

If anyone can perhaps help that would be much appreciated.

3
  • Please show us some code, SE is not a newspaper Commented Apr 12, 2013 at 17:00
  • Show us the code you've tried to use thus far Commented Apr 12, 2013 at 17:00
  • This is what I have so far.. while($row = mysql_fetch_array($query)){ $names[$loop]= $row['teamName']; $loop++; } Commented Apr 12, 2013 at 17:03

2 Answers 2

1

Do you mean something like this?

$i = 0;
$my_array = array();

while ($row = mysql_fetch_assoc($result))
{
    $my_array[$i]['name'] = $row['names'];
    $my_array[$i]['otherfield'] = $row['otherfield'];
    $i++;
}

now you can do something like this

echo $my_array[2]['name'];
Sign up to request clarification or add additional context in comments.

3 Comments

That looks like what I want to do! I'm no longer at my computer at the moment but will test it when I get back to it. I appreciate your help with it this far!
No problem ;) I typed it here on the fly so beware of typos and syntax errors. But you should get the general idea. Oh and mysql_fetch_assoc is short for mysql_fetch_array($result, MYSQL_ASSOC)
mysql_fetch_assoc is deprecated and you should be using mysqli_fetch_assoc
0

Simply do :

$rows = [];
while($row = mysql_fetch_assoc($result)) {
    $rows[] = $row;
}

1 Comment

mysql_fetch_assoc is deprecated and you should be using mysqli_fetch_assoc

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.