populate an array from mssql query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sfoley77
Forum Newbie
Posts: 15
Joined: Tue Jun 02, 2009 10:13 am

populate an array from mssql query

Post 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.
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: populate an array from mssql query

Post 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
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: populate an array from mssql query

Post 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'];
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: populate an array from mssql query

Post 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
Post Reply