Fill in a PHP array with MySQL Data

Posted on 2013-02-06
Last Modified: 2013-02-07
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',





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?
Question by:jj1103
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 13

Accepted Solution

haloexpertsexchange earned 300 total points
ID: 38861533
if $fields is the array that you want filled with the data from the database you should do it like this.
Add this outside of while loop.
$fields = array();

$fields[] = array(
      'txtLastName'    => $LastName,
      'txtFirstName' => 'TEST',
      'txtMiddleName' => 'TEST',

What you should end up with at the end of the while loop is $fields as a two dimensional array.
So to access the first record you do it like this $fields[0]['txtLastName'].
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 200 total points
ID: 38862199
First off, you shouldn't be using mysql stuff anymore - it's deprecated. You should be using mysqli or PDO. Here's a PDO way of creating an array from the database records. The array keys will be the field names selected from your database:

//Connect to your database
$conn = new PDO('mysql:host=localhost;dbname=yourDb', 'username', 'password');

//Prepare the query
$query = $conn->prepare('SELECT * FROM employees WHERE accesscode = :accesscode');
$query->bindParam(':accesscode', $accesscode);
$accesscode = 'Whatever';

//Execute the query

//Load the results into an associative array
$results = $query->fetchAll(PDO::FETCH_ASSOC);

//Lets see what we got

Open in new window

Any questions about it, just ask.

Author Comment

ID: 38863607
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'];
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 200 total points
ID: 38863628
OK. Not sure what you mean by relative array!

Are txtLastName, txtFirstName and txtMiddleName the names of the fields in your database. If they are, then adjust the query accordingly:

$query = $conn->prepare('SELECT txtLastName, txtFirstName txtMiddleName FROM employees WHERE accesscode = :accesscode');

Open in new window

When you run my code you will end up with an array of arrays - each one having three keys - txtLastName, txtFirstName and txtMiddleName with the values from your database

Author Comment

ID: 38863675
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 :)

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question