craygo Posted October 11, 2006 Share Posted October 11, 2006 I have a table and I want to put a couple fields into an array. I would like to query the table with my parameters then fill the array the keys will be one field and the values will be another field. I tried this but it is not correct[code]<?php$accounts = array();$sql = "SELECT * FROM tokayacct"; $res = mysql_query($sql) or die (mysql_error()); while($r=mysql_fetch_assoc($res)){$accounts[0] = $r['account_num'];$accounts[1] = $r['meter_num'];}?>[/code]thanks guysRay Quote Link to comment https://forums.phpfreaks.com/topic/23699-solvedfill-an-array/ Share on other sites More sharing options...
Jocka Posted October 11, 2006 Share Posted October 11, 2006 ok so account_num , meter_num , are in your table. You want this stored in an array.. maybe store it by id and put another array in there.you could use something like:[code]<?php$accounts = array();$sql = "SELECT * FROM tokayacct"; $res = mysql_query($sql) or die (mysql_error()); while($r=mysql_fetch_assoc($res)){ $accounts[$r[id]] = array($r['account_num'], $r['meter_num']);}?>[/code]something like that might work.. Quote Link to comment https://forums.phpfreaks.com/topic/23699-solvedfill-an-array/#findComment-107585 Share on other sites More sharing options...
kenrbnsn Posted October 11, 2006 Share Posted October 11, 2006 The reason the OP's original code does not work is that the OP is constantly overlaying the first and second elements in the array.Another way of doing this would be:[code]<?php$accounts = array();$sql = "SELECT * FROM tokayacct"; $res = mysql_query($sql) or die (mysql_error()); while($r=mysql_fetch_assoc($res)){ $accounts[$r['account_num']] = $r['meter_num'];}?>[/code]Which builds an array that is keyed by the account_number.Ken Quote Link to comment https://forums.phpfreaks.com/topic/23699-solvedfill-an-array/#findComment-107587 Share on other sites More sharing options...
printf Posted October 11, 2006 Share Posted October 11, 2006 <?phpMaybe like...[code]<?$accounts = array ();$sql = "SELECT * FROM tokayacct";$res = mysql_query ( $sql ) or die ( mysql_error () );while ( $r = mysql_fetch_assoc ( $res ) ){ $accounts[$r['account_num']] = $r['meter_num'];}?>[/code]me! Quote Link to comment https://forums.phpfreaks.com/topic/23699-solvedfill-an-array/#findComment-107588 Share on other sites More sharing options...
mainewoods Posted October 11, 2006 Share Posted October 11, 2006 Make a 2 dimensional array:[code]<?php$accounts = array();$currow = 0; //initialize$sql = "SELECT * FROM tokayacct"; $res = mysql_query($sql) or die (mysql_error()); while($r=mysql_fetch_assoc($res)){ //2 dimensional array notation used below $accounts[$currow]['account_num'] = $r['account_num']; $accounts[$currow]['meter_num'] = $r['meter_num']; //add other fields you want to store here $currow++; //increment first array index }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23699-solvedfill-an-array/#findComment-107595 Share on other sites More sharing options...
mainewoods Posted October 11, 2006 Share Posted October 11, 2006 -- Quote Link to comment https://forums.phpfreaks.com/topic/23699-solvedfill-an-array/#findComment-107598 Share on other sites More sharing options...
craygo Posted October 12, 2006 Author Share Posted October 12, 2006 Thanks for the replies fellas. Kens worked great.Ray Quote Link to comment https://forums.phpfreaks.com/topic/23699-solvedfill-an-array/#findComment-108040 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.