Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Fill in a PHP array with MySQL Data

Posted on 2013-02-06
Medium Priority
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 1200 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 44

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 800 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 44

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 800 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

618 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