Jump to content

MySql fetch array


bigheadedd

Recommended Posts

Hi,

 

I'm wondering if theres a shortcut to a potential problem I have.

I'm currently running a query on my website to pull all the fields from a table in my database, for the data to be used on various parts of the page.

 

Usually I would do something as follows

 

$result = mysql_query("SELECT * FROM table WHERE page='1'");

while ($row=mysql_fetch_array($result))
{
$title = $row["title"];
$data = $row["data"];
}

 

And so on and so forth. I would then call the appropriate data by echoing $data for example.

However, my table contains a lot more rows than i've mentioned (Around 25 or so). Rather than assigning each to a variable and having a large portion of variable assignments at the top of the page, is there any clever way of putting all of these values inside of an array.

So for example, I could call $array_data["title"] or $array_data["data"]??

So it keeps the same key, but puts it inside of an array that I don't have to loop through each time?

 

Hope that makes sense!

 

Thanks,

Edd

 

Link to comment
Share on other sites

Really?

At the moment, I call $row["data"] for example, but it has to be within a mysql_fetch_array() function/loop.

What i'm wondering, is that rather than looping through, resetting the query to the beginning etc each time, be able to put it inside of an array that functions the same as $row["data"], but not within the mysql loop??

 

I think that makes sense..?

Link to comment
Share on other sites

Hi,

 

Ah yes, sorry!

I've actually figured out a way of doing what I wanted to do.

 

Rather than doing multiple while loops using mysql_fetch_array, or avoiding using a large one at the beginning (and assigning all of the information to variables), I wanted to put it all into an array with the same keys so I could call it without using loops.

 

The code I came up with is:

 

while ($data[]=mysql_fetch_assoc($tresult));

echo $data[0]["title"];
echo $data[0]["information"];

 

etc etc. I've realised this is what I needed, but never tried it before. Of course, if theres a better way, let me know :)

 

Thanks!

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.