Solved

Fill in a PHP array with MySQL Data

Posted on 2013-02-06
5
587 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
Comment Utility
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 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 200 total points
Comment Utility
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
Comment Utility
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 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 200 total points
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
This article discusses four methods for overlaying images in a container on a web page
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…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now