Peggy Posted August 28, 2009 Share Posted August 28, 2009 I have a form with multiple pages/forms. Some of these forms have up to 40 check boxes. I know how to put the information into the data base easily with out having to name each field because the information is writen to the post array this code works-right. foreach($_POST as $key => $value){ } So I am wondering if three is an array that the information from the data base is written to. I know how to write the code as follows: if(!empty($applicant_id)){ $sql = 'SELECT * FROM form2 WHERE applicant_id = ' .$applicant_id; $result = mysql_query($sql,$db) or die(mysql_error(). "<br />SQL: $sql"); $row = mysql_fetch_array($result); $applicant_telephone = $row['applicant_telephone']; $applicant_email = $row['applicant_email']; $applicant_id = $row['applicant_id']; } Quote Link to comment https://forums.phpfreaks.com/topic/172225-solved-retreving-large-amounts-of-data-from-database/ Share on other sites More sharing options...
DavidAM Posted August 28, 2009 Share Posted August 28, 2009 In your code, $row is the array the data is written to. I don't think there is a Super Global (like $_POST) that the data goes to. However, you can use your $row just like you did $_POST: foreach ($row as $key => $value) { /* even use "variable-variables to do the assignments (as long as the column names are all valid variable names */ $$key = $value; } Notice the two dollar-signs on key ($$key = $value). That will use the value in $key as the variable name. So it's the same as the last three lines of code in your example: $applicant_telephone = $row['applicant_telephone']; $applicant_email = $row['applicant_email']; $applicant_id = $row['applicant_id']; Quote Link to comment https://forums.phpfreaks.com/topic/172225-solved-retreving-large-amounts-of-data-from-database/#findComment-908055 Share on other sites More sharing options...
Peggy Posted August 28, 2009 Author Share Posted August 28, 2009 Thank-you!! :) Quote Link to comment https://forums.phpfreaks.com/topic/172225-solved-retreving-large-amounts-of-data-from-database/#findComment-908058 Share on other sites More sharing options...
Peggy Posted August 28, 2009 Author Share Posted August 28, 2009 The $$key = $value; is really great information!!! You have saved me lots of time!! Quote Link to comment https://forums.phpfreaks.com/topic/172225-solved-retreving-large-amounts-of-data-from-database/#findComment-908070 Share on other sites More sharing options...
DavidAM Posted August 28, 2009 Share Posted August 28, 2009 Glad to help. I've never actually used that approach, but answering your question got me to thinking about how it might be useful. Just be sure that your column names are valid for variable names and that they don't collide with existing variables. If the value in $key is not a valid variable name, you should get an error message (I think). If it is the same as an existing variable name, it will just replace the contents of that variable (without warning) which might be just what you want. You might want to look at the extract function (http://us3.php.net/manual/en/function.extract.php). It does the same as the foreach example but allows you to add a prefix to the resulting variable names, prevent collisions, etc. Quote Link to comment https://forums.phpfreaks.com/topic/172225-solved-retreving-large-amounts-of-data-from-database/#findComment-908085 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.