Jump to content

Archived

This topic is now archived and is closed to further replies.

gladiator83x

arrays in php and mysql

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?

Share this post


Link to post
Share on other sites
Do you mean the entire row as an array? If you want an associative array, you should be able to use $line as is.

Share this post


Link to post
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'].

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.