October 30, 200917 yr I'm trying to access info from a MySQL database. I have a string that contains the column name of the information I want. Which works fine. But I want to go through a loop, one query per loop and each time the loop increments one, have the string hold a different column name taken from an array. This is where I come unstuck! The below code works, but when I replace $result = mysql_query("SELECT ".$send1." FROM `flash_diary_log` WHERE username = 'admin'"); for $result = mysql_query("SELECT ".$send[$n]." FROM `flash_diary_log` WHERE username = 'admin'"); it stops working. :-( $send = array("day1_lock1","day1_lock2","day1_lock3","day1_lock4","day1_lock5","day1_lock6","day1_lock7","day1_lock8","day1_lock9","day1_lock10","day1_lock11","day1_lock12","day1_lock13","day1_lock14","day1_lock15","day1_lock16"); $send1 = "day1_lock1"; for($n=0; $n<16; $n++) { // Query a MySQL table in the selected database $result = mysql_query("SELECT ".$send1." FROM `flash_diary_log` WHERE username = 'admin'"); if (!$result) { die("Query to show fields from table failed"); }; $row = mysql_fetch_row($result); foreach($row as $cell) $addtoVar .=",".$cell; };
October 30, 200917 yr you want a foreach loop so: $send = array("day1_lock1","day1_lock2","day1_lock3","day1_lock4","day1_lock5","day1_lock6","day1_lock7","day1_lock8","day1_lock9","day1_lock10","day1_lock11","day1_lock12","day1_lock13","day1_lock14","day1_lock15","day1_lock16"); foreach($send as $send_n){ // Query a MySQL table in the selected database $result = mysql_query("SELECT ".$send_n." FROM `flash_diary_log` WHERE username = 'admin'"); if (!$result) { die("Query to show fields from table failed"); }; $row = mysql_fetch_row($result); foreach($row as $cell) $addtoVar .=",".$cell; }; }; this should work I think. It'll loop through the whole array.
October 30, 200917 yr Author It WORKS! thank-you. I'm so happy I could cry. but I'm at work so I won't!
Create an account or sign in to comment