Jump to content

list mysql results in an array for easy call back


Sleeper

Recommended Posts

Lets start out by saying I'm a nube to sql/php things so I am learning as I go. I try to read all that I can before I post, and only post when I cant figure it out on my own.

 

That being said. What I want to do in simplest terms is be able to assign a variable to each item in an sql table. So say I have an mysql table that has ID, username, fontcolor. I want to be able to pull those out so say....

 

while($row = mysql_fetch_array($users)) {

$username[$i] = $row[username];
$fontcolor[$i] = $row[fontcolor];
}

 

Then on the page I can just call to $username[1] type thing. I have tried mixing this several different ways with for and while and I keep getting errors on the page. I realize the code isn't showing everything but its just there to show you the idea of what I'm trying to do. I just want it to generate a list(array) that will make it easier for me to call back just the items I need on parts of the page with out having to have extra coding everywhere.

 

Thanks in advance.

 

Jim

Ok well this is one version that I have tried..

 

$result= mysql_query("SELECT * FROM users") or die(mysql_error());
$rowCheck = mysql_num_rows($result);
$limit = ($rowCheck -1);

$menu = mysql_query("SELECT username,fontcolor FROM users ORDER BY SO ASC Limit $limit") or die(mysql_error());
while($row = mysql_fetch_array($menu)) {

for ($i=0; $i <= $limit; $i++) {
$txt[$i]=$row[username];
$link[$i]=$row[fontcolor ];
}}

echo $txt[5], $link[5];
mysql_close($con);

 

Only problem with that is it only brings back one value even though $limit is echoing out at 9 but its only bringing up the last option not the first 8.

I assume the code you posted is probably just example code, but it is flawed

while($row = mysql_fetch_array($users)) {
$username[$i] = $row[username];
$fontcolor[$i] = $row[fontcolor];
}

 

One, $i is not defined or incremented

Two, you are not enclosing the indexes (i.e. db field names) in quotes. Yes, it will wor, but it is not efficient.

 

I will also add that, most likely, you should not need (or want) to do what you are asking. Unless you need to process the database results multiple times, dumping the data into an array only increases the overhead. Some new programmers have difficulty in efficiently working with database results and fall back to using arrays.

 

But, if you really need to use an array, here is how I would handle it

while($row = mysql_fetch_array($users))
{
    $result[] = $row;
}

 

You could then reference the third fontcolor using $results[2]['fontcolor']

Ty mjdamato. That worked perfectly. I was not aware that you could just put in the [] and it would auto value. I though I would have to put in the $i type situation so it would keep adding.

 

I do not rely on arrays very often. However what I'm building at this time is definitely something that will save on resources by doing it this way.

 

Thanks again.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.