Jump to content

[SOLVED] Retreving Large amounts of data from database


Peggy

Recommended Posts

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'];

 

}

 

:)

 

 

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'];

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.

 

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.