I have a mySQL table containing, say, 20 rows and 10 columns. I want to pull the entire mySQl table into a php array so that I can call the values out of the array instead of the mySQL table. I know, it sounds dumb at first. I'm using these values in a loop that will be hit thousands of times per use, so I want to minimize calls to the server and have it all happen within the script.
Feel free to stop me at any time and offer a better suggestion or tell me I have the completely wrong idea about this. Before going any further, correct me if I'm wrong. If I want to go through a loop about 1000 times (or 100,000 for that matter), each time changing the variables based on mySQL table entries, I assume it would be better to have the values in an associative, 2-D array rather than a mySQL table. That way I can avoid calling an $sql statement every iteration and instead use something like $my_array[4][9]. This (by my educated guess) will minimize server traffic and save me some hosting fees and angry web hosts. Now, on to the rest...
So anyway, I have a table with lots of columns and even more rows. I want to, before my iterative loop, pull out all the values from the SQL table and store them in an array. Each row represents a set of variables for any given iteration. Thus, each iteration of a FOR loop will use each value in only one row. Then it'll go to the next row and use each value in that row at the next iteration. This sort of repetitious ballyhoo will continue until each row has been used, and then I'll be done and I'll move on to the next table and do it all over again. The reason? Don't ask.
Okay, so I want to build this array of table rows. The result will be an array like $my_array[rows][cols]. How will I do this? That's where I'm a bit foggy. Here's a quick look into my mind regarding this issue. It may look something like this:
Code: Select all
for($i=0; $i<=$num_rows; $i++){
$sql="SELECT * FROM table WHERE row=$i";
$result=mysql_query($sql);
$my_array=$my_array[.$i.][mysql_result($result,0,$i)];
}