Sleeper Posted December 5, 2010 Share Posted December 5, 2010 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 Link to comment https://forums.phpfreaks.com/topic/220742-list-mysql-results-in-an-array-for-easy-call-back/ Share on other sites More sharing options...
litebearer Posted December 5, 2010 Share Posted December 5, 2010 Simple answer is YES the data can be assigned to variables (inc arrays) .We would need to see morte of your code to see where and why your errors are occuring. Link to comment https://forums.phpfreaks.com/topic/220742-list-mysql-results-in-an-array-for-easy-call-back/#findComment-1143217 Share on other sites More sharing options...
Sleeper Posted December 5, 2010 Author Share Posted December 5, 2010 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. Link to comment https://forums.phpfreaks.com/topic/220742-list-mysql-results-in-an-array-for-easy-call-back/#findComment-1143220 Share on other sites More sharing options...
Psycho Posted December 5, 2010 Share Posted December 5, 2010 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'] Link to comment https://forums.phpfreaks.com/topic/220742-list-mysql-results-in-an-array-for-easy-call-back/#findComment-1143223 Share on other sites More sharing options...
Sleeper Posted December 5, 2010 Author Share Posted December 5, 2010 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. Link to comment https://forums.phpfreaks.com/topic/220742-list-mysql-results-in-an-array-for-easy-call-back/#findComment-1143224 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.