Jump to content

Recommended Posts

Hi All,

I created a new table called Defects, which has 4 columns. I am trying to save the entire column as an array...is that possible. This is what I have tried.
------------------------------------------------------
$array= "SELECT Review_Defects.Defects FROM Review_Defects";
$array_result= mysql_query($array) or die('Query failed: ' .mysql_error());
while ($line = mysql_fetch_array($array_result, MYSQL_ASSOC)) {
foreach ($line as $col_value){
// echo $col_value;
}

echo  $col_value; //--echoes everything in column
}
//echo  $col_value; --just echoes the last thing in column
---------------------------------------------------------
If I echo within the loop, I receive everything inside the column--which is what I want within my array. Is there some sort of function that I will have to use?
Link to comment
https://forums.phpfreaks.com/topic/15741-arrays-in-php-and-mysql/
Share on other sites

From my understanding, you want the row to be an array and it already is. The array is associative, using column names as keys. For example, if your query was [tt]select id, name from usr[/tt], your array is going to look something like...
[tt]
id => 1,
name => bob
[/tt]
...which you access by $line['id'] and $line['name'].
Try
[code]<?php

$defects = array();

$array= "SELECT Review_Defects.Defects FROM Review_Defects";
$array_result= mysql_query($array) or die('Query failed: ' .mysql_error());
while ($line = mysql_fetch_array($array_result, MYSQL_ASSOC)) {
  $defects[] = $line['Defects'];  // store value in array
}

// view the arraye
echo '<pre>', print_r($defects, true), '</pre>';
?>[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.