Solved

Fill in a PHP array with MySQL Data

Posted on 2013-02-06
5
613 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
[X]
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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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. …
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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).

730 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