Jump to content


Photo

SQL Query and Arrays


  • Please log in to reply
2 replies to this topic

#1 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 19 June 2006 - 04:27 PM

Hello,

I am trying to get information from a sequel query and place it into 2 different arrays based on a test. I am pulling two values from the table valueA and valueB, if valueB is a certain number then valueA gets written to one new array and if the test fails then it is written to the other new array. here is the code:-

<?php

//create new variable arrays
$arr1 = array();
$arr2 = array();

// connect to database , create and execute query
$query = 'SELECT valueA, valueB FROM table';

$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());

// check if records were returned
if (mysql_num_rows($result) > 0)
{
//iterate over record set
[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]while($row = mysql_fetch_row($result))
{
if ($row['valueB' == '1')
{
$arr1[] = $row['valueA']; [!--coloro:#000000--][span style=\"color:#000000\"][!--/coloro--]//will this bit work??[!--colorc--][/span][!--/colorc--]
}
else
{
$arr2[] = $row['valueA'];
}
}[!--colorc--][/span][!--/colorc--]
}
else
{
// print error message
echo 'No rows found!';
}

// close connection to MySQL server
mysql_close($connection);

?>

It seems to run ok and places something into the relevant arrays but when I try and view whats in the arrays it tells me the block numbers of the arrays but the value is blank, as though it has placed a NULL value in there....I am now confused..thanks in advance

valueA is date object valueB is a number so what I am expecting is two arrays, both with dates in them, one corresponding to a row value of ones and the other to a row value of anything else.
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#2 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 19 June 2006 - 04:43 PM

Add this to the end of your script and see what you get


print_r($arr1);
echo "<br/><br/>";
print_r($arr2);




#3 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 20 June 2006 - 07:54 AM

thanks for the help, I figured out my problem and it was tipping urror on my behalf, when you initialise a variable called "adate" and then try and refernece a variable called "aDate" it doesnt seem to work. how daft can one person be???

(rhetorical question - no actual answer required)
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users