Page 1 of 1

populate an array from mssql query

Posted: Tue Jul 28, 2009 9:45 am
by sfoley77
Hi all, I am trying to add some data from a mssql query to an array variable called freeTrialCodes, the query is executing fine but I trying to figure how to populate the array with the data in result from query, the key value that I need is CONF_VALUE below is the code

Code: Select all

$query = "SELECT * ";
$query .= "FROM OAG_SC_DOMAIN D ";
$query .= "JOIN OAG_SC_CONFIGURATION C "; 
$query .= "ON D.DOMAIN_ID = C.DOMAIN_ID "; 
$query .= "WHERE D.DOMAIN_ID = 12"; 
 
//execute the SQL query and return records
$result = mssql_query($query);
 
$numRows = mssql_num_rows($result); 
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>"; 
 
$s = mssql_fetch_array($result);
 
 
//display the results 
while($row = mssql_fetch_array($result))
{
  echo "result: " . $row["DOMAIN_CD"] . $row["CONF_VALUE"] . "\n";
  
}
 
//What I need to do here is populate the data from the resultset to an
// array called FreeTrialCodes
 
 

This is what the query looks like in tables
please advise


conf_id domain_cd conf_key conf_value Conf_description
85 12 1648 1648 FREE_TRIAL_ORDER_CODE_IDS Order Code IDs for free trials.
86 12 1645 1645 FREE_TRIAL_ORDER_CODE_IDS Order Code IDs for free trials.
87 12 1433 1433 FREE_TRIAL_ORDER_CODE_IDS Order Code IDs for free trials.
88 12 1420 1420 FREE_TRIAL_ORDER_CODE_IDS Order Code IDs for free trials.
89 12 1405 1405 FREE_TRIAL_ORDER_CODE_IDS Order Code IDs for free trials.
90 12 1661 1661 FREE_TRIAL_ORDER_CODE_IDS Order Code IDs for free trials.

Re: populate an array from mssql query

Posted: Tue Jul 28, 2009 10:32 am
by spider.nick

Code: Select all

$output = array(array());
$count = -1;
while( $row = mysql_fetch_array($res) )
     $output[$count++]['CONF_VALUE'] = $row['CONF_VALUE'];
Nick

Re: populate an array from mssql query

Posted: Tue Jul 28, 2009 10:50 am
by jayshields
Just a FYI, you could do this (seems more intuitive to me):

Code: Select all

$output = array(array());
$count = 0;
while( $row = mysql_fetch_array($res) )
     $output[++$count]['CONF_VALUE'] = $row['CONF_VALUE'];

Re: populate an array from mssql query

Posted: Tue Jul 28, 2009 10:54 am
by spider.nick
jayshields wrote:Just a FYI, you could do this (seems more intuitive to me):

Code: Select all

$output = array(array());
$count = 0;
while( $row = mysql_fetch_array($res) )
     $output[++$count]['CONF_VALUE'] = $row['CONF_VALUE'];
Actually, you are partly right. Might should read:

Code: Select all

$output = array(array());
$count = 0;
while( $row = mysql_fetch_array($res) )
     $output[$count++]['CONF_VALUE'] = $row['CONF_VALUE'];
Jay, your is correct, except your array will start at an index of 1, instead of 0.

Nick