Jump to content

[SOLVED] mysql 3d array in php


c172cpt

Recommended Posts

i need help i cant figure out how to create a 3d array in php from a mysql query

 

the query is:

select name, file from images order by rand() limit 0,18

 

this gives me 18 random rows from the db and i i can sort though all of it with mysql_fetch_array but only one row at a time

what i want is all the data at once

eg.

$answer['row#']['name']['data']

$answer['row#']['file']['data']

 

essentially i want 36 variables from the one mysql query

 

thanks,

 

-scott-

Link to comment
Share on other sites

Hmm... Something like this?

 

<?php

$query = "SELECT name, file FROM images ORDER BY RAND() LIMIT 0,18";
$result = mysql_query($query);

$row = 0;
while ($record = mysql_fetch_assoc($result)) {
    $answer[$idx]['name'] = $record['name'];
    $answer[$idx]['file'] = $record['file'];
    $row++;
}
?>

Link to comment
Share on other sites

This should work as well, but I like th one above better as it is apparent exactly what variables are being set. But this one should be a little faster if speed becomes an issue.

 

[code]<?php

$query = "SELECT name, file FROM images ORDER BY RAND() LIMIT 0,18";
$result = mysql_query($query);

while ($record = mysql_fetch_assoc($result)) {
    $answer[] = $record;
}
?>

[/code]

Link to comment
Share on other sites

When I was working out my 3d array issue, I tried something similiar to this, but it would only use the last entry instead of building a multi demension array.  I ended up doing something like your first example, it seemed more reliable and results were as expected.

 

This should work as well, but I like th one above better as it is apparent exactly what variables are being set. But this one should be a little faster if speed becomes an issue.

 

[code]<?php

$query = "SELECT name, file FROM images ORDER BY RAND() LIMIT 0,18";
$result = mysql_query($query);

while ($record = mysql_fetch_assoc($result)) {
    $answer[] = $record;
}
?>

[/code]

Link to comment
Share on other sites

The second code I posted will result in the exact same array. The fact that you had a problem previously has nothing to do with that proposed method.

 

Did you remember to put the empty brackets when declaring the value? That is what ensures that a new array item is created.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.