Jump to content


Photo

arrays in php and mysql


  • Please log in to reply
4 replies to this topic

#1 gladiator83x

gladiator83x
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 26 July 2006 - 09:01 PM

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?

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 26 July 2006 - 09:09 PM

Do you mean the entire row as an array? If you want an associative array, you should be able to use $line as is.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 gladiator83x

gladiator83x
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 26 July 2006 - 09:16 PM

How will I do that? I know that when I try to echo out $line, it just outputs 'Array' numerous times

#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 26 July 2006 - 09:20 PM

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 select id, name from usr, your array is going to look something like...

id => 1,
name => bob

...which you access by $line['id'] and $line['name'].

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 26 July 2006 - 09:27 PM

Try
<?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>';
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users