Link to home
Start Free TrialLog in
Avatar of jj1103
jj1103

asked on

Fill in a PHP array with MySQL Data

I have an array that I'd like to fill in with data from a MySQL Database.  It works great when I enter the data inside the ' ' but when I fetch the data from the database using $row = mysql_fetch_array() it breaks.

//mysql_connect info here//
$result = mysql_query("SELECT * FROM employees WHERE accesscode='" . $accesscode . "'");

while ($row = mysql_fetch_array($result)) {
$LastName = $row['LastName']; 
	
$fields = array(
	'txtLastName'    => $LastName,
	'txtFirstName' => 'TEST',
	'txtMiddleName' => 'TEST',


...
 <?php

}

mysql_close($conn);

?>

Open in new window


I guess I'm trying to place an array inside an array.  Is there a better way to autofill the $fields array() with data from the MySQL Database?
ASKER CERTIFIED SOLUTION
Avatar of haloexpertsexchange
haloexpertsexchange
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jj1103
jj1103

ASKER

Hi,
I tied both your recommendations. It comes up with blank field 0. These are specific fields I need to name (i.e. txtLastName) and not use a relative array.
$fields = array(
	'txtLastName'    => 'lastnamefromdatabase',
	'txtFirstName' => 'firstnamefromdatabase',
	'txtMiddleName' => 'middlenamefromdatabase'
);

Open in new window


The 'lastnamefromdatabase' should be something like  echo $row['LastName'];
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jj1103

ASKER

After playing around with it I decided to use the form page that collects all the database data and then sends it to another page where I can collect it into an array and it worked well.

Thank-you for your assistance in helping me think outside the code :)