Jump to content

foreach with MySQL Field Names


Harlequin

Recommended Posts

Hi.

 

The idea:

 

I want to pull a client record and simply place all the row columns into editable form field text boxes. Once the user reviews and edits a submit button will update the record.

 

What I have:

 

$Query01 = "SELECT * FROM `CustomerSignups`
WHERE `Id` = '$_GET[id]'";
$Result01 = mysql_query($Query01) or die("Error 01: " . mysql_error());

	while ($get_info = mysql_fetch_row($Result01))
	{
	foreach ($get_info as $field)
	echo "<p>$field</p>";

 

It displays as expected, a line for each field.

 

How can I get this to pull the field names also and then I can use those as text box input fields...?

Link to comment
Share on other sites

Well, he'll need to change a few things to make this work properly, if he wants to use the foreach method.

<?php
$Query01 = "SELECT * FROM `CustomerSignups` WHERE `Id` = '$_GET[id]'";
$Result01 = mysql_query($Query01) or die("Error 01: " . mysql_error());
while ($get_info = mysql_fetch_assoc($Result01))
{
	// you need to turn get info into an array, since it's already been looped.
	$get_info[] = $get_info;
}
// we need to end the while loop before foreach loop, if you didn't you'd be looping the same data twice.
foreach ($get_info as $key => $value)
{
	echo "<p>$key - $value</p>";
}
?>

I changed mysql_fetch_row to mysql_fetch_assoc.

I ended the while loop before the foreach loop.

I printed your key and value for you, so you can have a good idea on how to go about doing the rest of this on your own.

If you need any in-depth explanations, feel free to ask.

Link to comment
Share on other sites

Mmmm...

 

OK, I get what you're doing and inserted the code like so:

 

$Query01 = "SELECT * FROM `CustomerSignups` WHERE `Id` = '$_GET[id]'";
$Result01 = mysql_query($Query01) or die("Error 01: " . mysql_error());

	while ($get_info = mysql_fetch_assoc($Result01))
	{
	$get_info[] = $get_info;
	}
	foreach ($get_info as $key => $value)
	{
	echo "<p>$key - $value</p>";
	}
}

 

But I get this:

 

Warning: Invalid argument supplied for foreach() in /home/... : eval()'d code on line 18

 

This is line 18:

 

foreach ($get_info as $key => $value)

 

But I can't see where the problem is.

 

Obviously I'm going to perservere with this as my old way of doing it, calling out each table field individually is simply a waste of time and won't include new table fields.

Link to comment
Share on other sites

I see what you're saying, I tried with code with my own script and it works 100%.

<?php
$Query01 = "SELECT * FROM `CustomerSignups` WHERE `Id` = '$_GET[id]'";
$Result01 = mysql_query($Query01) or die("Error 01: " . mysql_error());

while ($get_info = mysql_fetch_assoc($Result01))
{
$info[] = $get_info;
}
foreach ($info as $field)
{
foreach ($field AS $key=>$value)
{
	echo "{$key} - {$value}<br/>";
}
}
?>

 

It will show the field NAME then it's VALUE next to it. Hope this is what you wanted.

Link to comment
Share on other sites

By storing the data in an intermediate array and looping over that array, when you are just going to output all the results anyway, you are causing that portion of the code to use twice as much memory (the data is already stored in memory in the result set) and you are causing that portion of the code to take twice as long to execute.


<?php
$Query01 = "SELECT * FROM `CustomerSignups` WHERE `Id` = '$_GET[id]'";
$Result01 = mysql_query($Query01) or die("Error 01: " . mysql_error());

while ($get_info = mysql_fetch_assoc($Result01))
{
foreach ($get_info AS $key=>$value)
{
	echo "{$key} - {$value}<br/>";
}
}
?>

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.