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

 

}

 

:)

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.