Solved

Fill in a PHP array with MySQL Data

Posted on 2013-02-06
5
607 Views
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',


...
 <?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?
0
Comment
Question by:jj1103
  • 2
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
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'].
0
 
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:

<?php 
//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
$query->execute();

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

//Lets see what we got
print_r($results);
?>

Open in new window

Any questions about it, just ask.
0
 

Author Comment

by:jj1103
ID: 38863607
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'];
0
 
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
0
 

Author Comment

by:jj1103
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 :)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
facebook login problem :- phonegap mobile app - what to enter for "Valid OAuth redirect URIs" 1 40
IP 10.0.1.2 / 255.0.0.0 61 56
Link failure 16 32
JQuery Search Filter 2 28
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

840 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